Path.GetFileName 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| GetFileName(ReadOnlySpan<Char>) |
傳回以唯讀字元範圍表示之檔案路徑的檔名和擴展名。 |
| GetFileName(String) |
傳回指定路徑字串的檔名和擴展名。 |
GetFileName(ReadOnlySpan<Char>)
- 來源:
- Path.cs
- 來源:
- Path.cs
- 來源:
- 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節點末尾(由 回傳者回傳GetPathRoot)後面的字path元,以較晚者為準。 如果 path 以目錄分隔符結尾或僅由根節點組成,則該方法回傳 ReadOnlySpan<T>.Empty。 若 path 不包含目錄分隔字元或根節點,則該方法回傳 path。
在 Windows 上,根節點可以是例如磁碟機或 UNC 共享。 關於其他根格式,如 DOS 裝置路徑,請參見 Windows 系統的檔案路徑格式。 在 C:file.txt中,根節點為 C:,因此檔名為 file.txt。 像 \\server\share UNC 根節點會包含目錄分隔符,但它們是根節點的一部分,不會標記檔名的起始。 因此 中的 \\server\share\file.txt 檔名是 file.txt,而 中的 \\server\share 檔名是空的,因為整個路徑都是根節點。
另請參閱
適用於
GetFileName(String)
- 來源:
- Path.cs
- 來源:
- Path.cs
- 來源:
- 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根節點的字元,以較後者為準,或 null 若 path 為 null。
例外狀況
.NET Framework 與 .NET Core 版本早於 2.1: path 包含一個或多個定義於 GetInvalidPathChars().
範例
以下範例展示了 GetFileName 方法在基於 Windows 的桌面平台上的行為。
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 ''
備註
若檔案路徑為 null,則回傳值為 null 。
回傳字串包含的字元,這些字 path 元緊接在最後 DirectorySeparatorChar 一個字元或 AltDirectorySeparatorChar 根 path 的結尾(由 回傳者回傳 GetPathRoot),以後面為準。 如果 path 以目錄分隔符結尾或僅由根節點組成,則該方法回傳 String.Empty。 若 path 不包含目錄分隔字元或根節點,則該方法回傳 path。
在 Windows 上,根節點可以是例如磁碟機或 UNC 共享。 關於其他根格式,如 DOS 裝置路徑,請參見 Windows 系統的檔案路徑格式。 在 C:file.txt中,根節點為 C:,因此檔名為 file.txt。 像 \\server\share UNC 根節點會包含目錄分隔符,但它們是根節點的一部分,不會標記檔名的起始。 因此 中的 \\server\share\file.txt 檔名是 file.txt,而 中的 \\server\share 檔名是空的,因為整個路徑都是根節點。
由於 \ 是 Unix 上的合法檔案名稱,GetFileName 在 Unix 平台運行時,無法正確從基於 Windows 的路徑(如 C:\mydir\myfile.ext 回傳該檔案名稱,但在 Windows 平台運行的 GetFileName 可以正確從 Unix 路徑回傳檔名,例如 /tmp/myfile.ext, 因此,GetFileName 方法在 Unix 與 Windows 平台上的行為並不完全相同。
關於常見 I/O 任務的清單,請參見 Common I/O 任務。