Path 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
對包含檔案或目錄路徑資訊的實例執行操作 String 。 這些作業會以跨平臺的方式執行。
public ref class Path abstract sealed
public ref class Path sealed
public static class Path
public sealed class Path
[System.Runtime.InteropServices.ComVisible(true)]
public static class Path
type Path = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type Path = class
Public Class Path
Public NotInheritable Class Path
- 繼承
-
Path
- 屬性
範例
以下範例展示了該 Path 類別中的一些主要成員。
using System;
using System.IO;
class Test
{
public static void Main()
{
string path1 = @"c:\temp\MyTest.txt";
string path2 = @"c:\temp\MyTest";
string path3 = @"temp";
if (Path.HasExtension(path1))
{
Console.WriteLine("{0} has an extension.", path1);
}
if (!Path.HasExtension(path2))
{
Console.WriteLine("{0} has no extension.", path2);
}
if (!Path.IsPathRooted(path3))
{
Console.WriteLine("The string {0} contains no root information.", path3);
}
Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3));
Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath());
Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName());
/* This code produces output similar to the following:
* c:\temp\MyTest.txt has an extension.
* c:\temp\MyTest has no extension.
* The string temp contains no root information.
* The full path of temp is D:\Documents and Settings\cliffc\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\temp.
* D:\Documents and Settings\cliffc\Local Settings\Temp\8\ is the location for temporary files.
* D:\Documents and Settings\cliffc\Local Settings\Temp\8\tmp3D.tmp is a file available for use.
*/
}
}
Imports System.IO
Public Class Test
Public Shared Sub Main()
Dim path1 As String = "c:\temp\MyTest.txt"
Dim path2 As String = "c:\temp\MyTest"
Dim path3 As String = "temp"
If Path.HasExtension(path1) Then
Console.WriteLine("{0} has an extension.", path1)
End If
If Path.HasExtension(path2) = False Then
Console.WriteLine("{0} has no extension.", path2)
End If
If Path.IsPathRooted(path3) = False Then
Console.WriteLine("The string {0} contains no root information.", path3)
End If
Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3))
Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath())
Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName())
' This code produces output similar to the following:
' c:\temp\MyTest.txt has an extension.
' c:\temp\MyTest has no extension.
' The string temp contains no root information.
' The full path of temp is D:\Documents and Settings\cliffc\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\temp.
' D:\Documents and Settings\cliffc\Local Settings\Temp\8\ is the location for temporary files.
' D:\Documents and Settings\cliffc\Local Settings\Temp\8\tmp3D.tmp is a file available for use.
End Sub
End Class
備註
路徑是提供檔案或目錄位置的字串。 路徑不一定指向磁碟上的位置;例如,路徑可能會對應至記憶體中或裝置上的位置。 路徑的確切格式是由目前的平臺所決定。 例如,在某些系統上,路徑可以從磁碟驅動器或磁碟驅動器號開始,而這個專案在其他系統中則不存在。 在某些系統上,檔案路徑可以包含延伸模組,指出儲存在檔案中的資訊類型。 擴展名的格式與平台相關;例如,某些系統會將擴充功能限制為三個字元(例如在較小的快閃記憶體上常用的 FAT16,以及在光學媒體上使用的舊版 ISO 9660),而其他則不會。 目前的平臺也會決定用來分隔路徑元素的字元集,以及指定路徑時無法使用的字元集。 由於這些差異,類別的 Path 欄位以及部分成員 Path 的具體行為都依賴於平台。
路徑可以包含絕對或相對位置資訊。 絕對路徑完全指定位置:不論目前的位置為何,都可以唯一識別檔案或目錄。 相對路徑會指定部分位置:尋找以相對路徑指定的檔案時,會使用目前的位置作為起點。 要確定目前的目錄,請呼叫 Directory.GetCurrentDirectory。
此類型也支援存取裝置名稱的檔案系統物件,例如「\\?\C:\」。 裝置名稱支援則在 .NET Framework 4.6.2 中加入。
欲了解更多關於 Windows 上檔案路徑格式的資訊,請參見 Windows 系統上的檔案路徑格式。
大多數 Path 類別成員不會與檔案系統互動,也不會驗證由路徑字串指定的檔案是否存在。
Path 修改路徑字串(如 ChangeExtension,)的類別成員不會影響檔案系統中檔案名稱。
部分Path成員會驗證指定路徑字串的內容,若字串包含在路徑字串中不符的字元(如方法回傳GetInvalidPathChars的字元定義),則拋出 。ArgumentException 例如,在基於 Windows 的桌面平台上,無效路徑字元可能包括引號(“)、小於(<)、大於(>)、pipe (|)、退格(\b)、null(\0)以及 Unicode 字元 16 至 18 和 20 至 25。 此驗證行為會因 .NET 版本而異:
在 .NET Framework 上:所有
Path選擇路徑作為參數的成員,如果偵測到無效路徑字元,會拋出 。ArgumentException在 .NET: GetFullPath 中,是唯一若字串包含無效路徑字元時拋出 的 ArgumentException 成員。
這個 Path 類別的成員讓你能快速且輕鬆地執行常見操作,例如判斷檔案副檔名是否屬於路徑,以及將兩個字串合併成一個路徑名稱。
類別中的所有成員 Path 都是靜態的,因此可以被呼叫而無需有路徑實例。
注意
在接受路徑做為輸入字串的成員中,該路徑的格式必須良好或引發例外狀況。 例如,如果路徑為完整,但開頭為空格,則路徑不會在 類別的方法中修剪。 因此,路徑的格式不正確,而且會引發例外狀況。 同樣地,路徑或路徑的組合不能完全限定兩次。 例如,在大部分情況下,“c:\temp c:\windows” 也會引發例外狀況。 使用接受路徑字串的方法時,請確定您的路徑格式良好。
在接受路徑的成員中,路徑可以參考檔案或只參考目錄。 指定的路徑也可以參考伺服器和共享名稱的相對路徑或通用命名慣例 (UNC) 路徑。 例如,下列所有都是可接受的路徑:
C# 中的「c:\\MyDir\\MyFile.txt」,或 Visual Basic 中的「c:\MyDir\MyFile.txt」。
C# 中的 “c:\\MyDir” 或 Visual Basic 中的 “c:\MyDir”。
C# 中的 “MyDir\\MySubdir” 或 Visual Basic 中的 “MyDir\MySubDir”。
C# 中的 “\\\\MyServer\\MyShare” 或 Visual Basic 中的 “\\MyServer\MyShare”。
由於所有這些作業都是在字串上執行,因此不可能在所有案例中驗證結果是否有效。 舉例來說,這個 GetExtension 方法解析你傳給它的字串,然後回傳該字串的延伸。 不過,這並不表示該擴展名的檔案存在於磁碟上。
關於常見 I/O 任務的清單,請參見 Common I/O 任務。
欄位
| 名稱 | Description |
|---|---|
| AltDirectorySeparatorChar |
提供平臺特定的替代字元,用來分隔路徑字串中的目錄層級,以反映階層式文件系統組織。 |
| DirectorySeparatorChar |
提供平臺特定字元,用來分隔路徑字串中的目錄層級,以反映階層式文件系統組織。 |
| InvalidPathChars |
已淘汰.
已淘汰.
提供平台專用的字元陣列,無法在傳遞給類別成員 Path 的路徑字串參數中指定。 |
| PathSeparator |
平臺特定的分隔符,用來分隔環境變數中的路徑字串。 |
| VolumeSeparatorChar |
提供平臺特定的磁碟區分隔符。 |