Path.GetFileName Path.GetFileName Path.GetFileName Path.GetFileName Method

Definition

Overloads

GetFileName(ReadOnlySpan<Char>) GetFileName(ReadOnlySpan<Char>) GetFileName(ReadOnlySpan<Char>) GetFileName(ReadOnlySpan<Char>)
GetFileName(String) GetFileName(String) GetFileName(String) GetFileName(String)

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

GetFileName(ReadOnlySpan<Char>) GetFileName(ReadOnlySpan<Char>) GetFileName(ReadOnlySpan<Char>) GetFileName(ReadOnlySpan<Char>)

public:
 static ReadOnlySpan<char> GetFileName(ReadOnlySpan<char> path);
public static ReadOnlySpan<char> GetFileName (ReadOnlySpan<char> path);
static member GetFileName : ReadOnlySpan<char> -> ReadOnlySpan<char>
Public Shared Function GetFileName (path As ReadOnlySpan(Of Char)) As ReadOnlySpan(Of Char)
Parameters
Returns

GetFileName(String) GetFileName(String) GetFileName(String) GetFileName(String)

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

public:
 static System::String ^ GetFileName(System::String ^ path);
public static string GetFileName (string path);
static member GetFileName : string -> string
Public Shared Function GetFileName (path As String) As String
Parameters
path
String String String String

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

Returns

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 null, this method returns null.

Exceptions

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

Examples

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 );

// This code produces output similar to the following:
//
// GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
// GetFileName('C:\mydir\') returns ''
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);

// This code produces output similar to the following:
//
// GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
// GetFileName('C:\mydir\') returns ''
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)

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

Remarks

The returned value is null if the file path is null.

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

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

See Also

Applies to