Path.GetFileName 方法

定義

多載

GetFileName(ReadOnlySpan<Char>)

傳回檔案路徑的檔案名稱和副檔名,此路徑以唯讀的字元範圍表示。

GetFileName(String)

傳回指定路徑字串的檔案名稱和副檔名。

GetFileName(ReadOnlySpan<Char>)

來源:
Path.cs
來源:
Path.cs
來源:
Path.cs

傳回檔案路徑的檔案名稱和副檔名,此路徑以唯讀的字元範圍表示。

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)

參數

path
ReadOnlySpan<Char>

包含路徑的唯讀範圍,這是取得檔案名稱和副檔名的路徑。

傳回

path 中最後目錄分隔符號字元之後的字元。

備註

傳回的只讀範圍包含 中最後一個分隔符後面路徑的 path字元。 如果中的 path 最後一個字元是磁碟區或目錄分隔符,此方法會傳 ReadOnlySpan<T>.Empty回 。 如果 path 不包含分隔符,則方法會傳 path回 。

另請參閱

適用於

GetFileName(String)

來源:
Path.cs
來源:
Path.cs
來源:
Path.cs

傳回指定路徑字串的檔案名稱和副檔名。

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

參數

path
String

要從中取得檔案名稱和副檔名的路徑字串。

傳回

path 中最後目錄分隔符號字元之後的字元。 如果 path 的最後一個字元是目錄或磁碟區分隔符號字元,這個方法會傳回 Empty。 如果 pathnull,這個方法會傳回 null

例外狀況

.NET Framework 和 2.1 之前的 .NET Core 版本:path包含 中GetInvalidPathChars()定義的一或多個無效字元。

範例

下列範例示範方法在 Windows 型桌面平台上的行為 GetFileName

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 ''

備註

如果檔案路徑為 ,則傳回的值 nullnull

用來判斷檔案名開頭的分隔符為 DirectorySeparatorCharAltDirectorySeparatorChar

由於 \ 是 Unix 上的合法檔名, GetFileName 所以在 Unix 型平臺上執行無法正確從 C:\mydir\myfile.ext 等 Windows 型路徑傳回檔名,但在 GetFileName Windows 平臺下執行,可以從 /tmp/myfile.ext 等 Unix 型路徑正確傳回檔名,因此方法的行為 GetFileName 不完全與 Unix 型和 Windows 型平臺相同。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於