This documentation is archived and is not being maintained.

Path.GetFileName Method

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

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

static String^ GetFileName (
	String^ path
public static String GetFileName (
	String path
public static function GetFileName (
	path : String
) : String



The path string from which to obtain the file name and extension.

Return Value

A String consisting of the characters after the last directory character in path. If the last character of path is a directory or volume separator character, this method returns Empty. If path is a null reference (Nothing in Visual Basic), this method returns a null reference (Nothing in Visual Basic).

Exception typeCondition


path contains one or more of the invalid characters defined in InvalidPathChars, or contains a wildcard character.

The returned value is a null reference (Nothing in Visual Basic) if the file path is a null reference (Nothing in Visual Basic).

The separator characters used to determine the start of the file name are DirectorySeparatorChar and AltDirectorySeparatorChar.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Retrieve the fully qualified path of a file.


Retrieve only the file name from a path.


Retrieve only the directory name from a path.


Change the extension of a file.


The following code example demonstrates the behavior of the GetFileName method on a Windows-based desktop platform.

String^ fileName = "C:\\mydir\\myfile.ext";
String^ path = "C:\\mydir\\";
String^ result;
result = Path::GetFileName( fileName );
Console::WriteLine( "GetFileName('{0}') returns '{1}'", fileName, result );
result = Path::GetFileName( path );
Console::WriteLine( "GetFileName('{0}') returns '{1}'", path, result );

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

result = Path.GetFileName(fileName);
Console.WriteLine("GetFileName('{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.GetFileName(fileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'", 
                  fileName, result);

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

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0