This documentation is archived and is not being maintained.

Path.GetFileNameWithoutExtension Method

Returns the file name of the specified path string without the extension.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public static string GetFileNameWithoutExtension (
	string path
)
public static String GetFileNameWithoutExtension (
	String path
)
public static function GetFileNameWithoutExtension (
	path : String
) : String
Not applicable.

Parameters

path

The path of the file.

Return Value

A String containing the string returned by GetFileName, minus the last period (.) and all characters following it.

Exception typeCondition

ArgumentException

path contains one or more of the invalid characters defined in GetInvalidPathChars.

This method does not verify that the path or file name exists.

For a list of common I/O tasks, see Common I/O Tasks.

The following code example demonstrates a use of the GetFileNameWithoutExtension method.

string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;

result = Path.GetFileNameWithoutExtension(fileName);
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", 
    fileName, result);

result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'", 
    path, result);

// This code produces output similar to the following:
//
// GetFileNameWithoutExtension('C:\mydir\myfile.ext') returns 'myfile'
// GetFileName('C:\mydir\') returns ''

String fileName = "C:\\mydir\\myfile.ext";
String path = "C:\\mydir\\";
String result;

result = Path.GetFileNameWithoutExtension(fileName);
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", 
    fileName, result);

result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'", 
    path, result);

var fileName : String = "C:\\mydir\\myfile.ext";
var path : String = "C:\\mydir\\";
var result : String;

result = Path.GetFileNameWithoutExtension(fileName);
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", 
                  fileName, result);

result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'", 
                  path, result);

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Show: