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 namespace System;
using namespace System::IO;
int 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() );
Console::WriteLine( "\r\nThe set of invalid characters in a path is:" );
Console::WriteLine( "(Note that the wildcard characters '*' and '?' are not invalid.):" );
Collections::IEnumerator^ myEnum = Path::InvalidPathChars->GetEnumerator();
while ( myEnum->MoveNext() )
{
Char c = *safe_cast<Char^>(myEnum->Current);
Console::WriteLine( c );
}
}
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。
.NET Core 1.1 和更新版本和 .NET Framework 4.6.2 和更新版本也支援存取屬於裝置名稱的文件系統物件,例如 “\\?\C:\”。
如需 Windows 上檔案路徑格式的詳細資訊,請參閱 Windows 系統上的檔案路徑格式。
類別的大部分 Path
成員不會與文件系統互動,也不會驗證路徑字串所指定的檔案是否存在。 Path
修改路徑字串的類別成員,例如 ChangeExtension,不會影響檔案系統中的檔名。
某些 Path
成員會驗證指定之路徑字串的內容,如果字串包含路徑字串中無效的字元,則擲 ArgumentException 回 ,如方法傳回的 GetInvalidPathChars 字元中所定義。 例如,在 Windows 桌面平臺上,無效的路徑字元可能包含引號 (“) 、小於 (<) 、大於 (>) 、管道 (|) 、退格 (\b) 、null (\0) ,以及 Unicode 字元 16 到 18 到 25。 此驗證行為會因 .NET 版本而異:
在 2.1 之前的 .NET Framework 和 .NET Core 版本上:如果成員偵測到無效的路徑字元,則接受路徑作為自變數的所有
Path
成員都會擲ArgumentException回 。在 .NET Core 2.1 和更新版本上: 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 工作的清單,請參閱 一般 I/O 工作。
欄位
AltDirectorySeparatorChar |
提供平台特定替代字元,用以在反映階層式檔案系統組織的路徑字串中來分隔目錄層級。 |
DirectorySeparatorChar |
提供平台特定字元,用以在反映階層式檔案系統組織的路徑字串中分隔目錄層級。 |
InvalidPathChars |
已淘汰.
已淘汰.
提供字元的平台特定陣列,這些字元不可以在傳遞給 Path 類別成員的路徑字串引數中指定。 |
PathSeparator |
平台特定分隔符號字元,用來分隔環境變數中的路徑字串。 |
VolumeSeparatorChar |
提供平台特定磁碟區分隔符號字元。 |
方法
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應