Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Path.GetFileName Method

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

[Visual Basic]
Public Shared Function GetFileName( _
   ByVal path As String _
) As String
public 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).


Exception Type Condition
ArgumentException path contains one or more of the invalid characters defined in InvalidPathChars.


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

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. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Retrieve the fully qualified path of a file. GetFullPath
Retrieve only the file name from a path. GetFileNameWithoutExtension
Retrieve only the directory name from a path. GetDirectoryName
Change the extension of a file. ChangeExtension


[Visual Basic, C#, C++] The following example demonstrates the behavior of the GetFileName method on a Windows-based desktop platform.

[Visual Basic] 
Dim fileName As String = "C:\mydir\myfile.ext"
Dim pathname As String = "C:\mydir\"
Dim result As String

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

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

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

result = Path::GetFileName(fileName);
Console::WriteLine(S"GetFileName('{0}') returns '{1}'", 
   fileName, result);

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Path Class | Path Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

© 2017 Microsoft