Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
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. |
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)
A read-only span that contains the path from which to obtain the file name and extension.
The characters after the last directory separator character in path
.
The returned read-only span contains the characters of the path that follow the last separator in path
. If the last character in path
is a volume or directory separator character, the method returns ReadOnlySpan<T>.Empty. If path
contains no separator character, the method returns path
.
Product | Versions |
---|---|
.NET | Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Standard | 2.1 |
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
The path string from which to obtain the file name and extension.
The characters after the last directory separator 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
.
.NET Framework and .NET Core versions older than 2.1: path
contains one or more of the invalid characters defined in GetInvalidPathChars().
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 ''
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.
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.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in