Path.GetFileName Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| Name | Description |
|---|---|
| GetFileName(ReadOnlySpan<Char>) |
Returns the file name and extension of a file path that is represented by a read-only character span. |
| GetFileName(String) |
Returns the file name and extension of the specified path string. |
GetFileName(ReadOnlySpan<Char>)
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- Path.cs
Returns the file name and extension of a file path that is represented by a read-only character span.
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
- path
- ReadOnlySpan<Char>
A read-only span that contains the path from which to obtain the file name and extension.
Returns
The characters that follow the last directory separator character or the root of path, whichever comes later.
Remarks
The returned read-only span contains the characters of path that follow either the last directory separator character or the end of the root of path (as returned by GetPathRoot), whichever comes later. If path ends with a directory separator character or consists only of a root, the method returns ReadOnlySpan<T>.Empty. If path contains neither a directory separator character nor a root, the method returns path.
On Windows, the root can be, for example, a drive or a UNC share. For other root formats, such as DOS device paths, see File path formats on Windows systems. In C:file.txt, the root is C:, so the file name is file.txt. A UNC root such as \\server\share contains directory separators, but they're part of the root and don't mark the start of the file name. So the file name in \\server\share\file.txt is file.txt, and the file name in \\server\share is empty, because the whole path is the root.
See also
Applies to
GetFileName(String)
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- Path.cs
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);
public static string? GetFileName(string? path);
static member GetFileName : string -> string
Public Shared Function GetFileName (path As String) As String
Parameters
- path
- String
The path string from which to obtain the file name and extension.
Returns
The characters that follow the last directory separator character or the root of path, whichever comes later, or null if path is null.
Exceptions
.NET Framework and .NET Core versions older than 2.1: path contains one or more of the invalid characters defined in GetInvalidPathChars().
Examples
The following 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 ''
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 returned string contains the characters of path that follow either the last DirectorySeparatorChar or AltDirectorySeparatorChar character or the end of the root of path (as returned by GetPathRoot), whichever comes later. If path ends with a directory separator character or consists only of a root, the method returns String.Empty. If path contains neither a directory separator character nor a root, the method returns path.
On Windows, the root can be, for example, a drive or a UNC share. For other root formats, such as DOS device paths, see File path formats on Windows systems. In C:file.txt, the root is C:, so the file name is file.txt. A UNC root such as \\server\share contains directory separators, but they're part of the root and don't mark the start of the file name. So the file name in \\server\share\file.txt is file.txt, and the file name in \\server\share is empty, because the whole path is the root.
Because \ is a legal file name on Unix, GetFileName running under Unix-based platforms cannot correctly return the file name from a Windows-based path like C:\mydir\myfile.ext, but GetFileName running under Windows-based platforms can correctly return the file name from a Unix-based path like /tmp/myfile.ext, so the behavior of the GetFileName method is not strictly the same on Unix-based and Windows-based platforms.
For a list of common I/O tasks, see Common I/O Tasks.
See also
- File path formats on Windows systems
- File and Stream I/O
- How to: Read Text from a File
- How to: Write Text to a File