Path 類別

定義

在含有檔案或目錄路徑資訊的 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

提供平台特定磁碟區分隔符號字元。

方法

ChangeExtension(String, String)

變更路徑字串的副檔名。

Combine(String, String)

將兩個字串合併為一個路徑。

Combine(String, String, String)

將三個字串合併為一個路徑。

Combine(String, String, String, String)

將四個字串合併為一個路徑。

Combine(String[])

將一個字串陣列合併為單一路徑。

EndsInDirectorySeparator(ReadOnlySpan<Char>)

傳回的值指出路徑 (指定為唯讀範圍) 是否以目錄分隔符號結束。

EndsInDirectorySeparator(String)

傳回的值指出所指定路徑是否以目錄分隔符號結束。

Exists(String)

判斷指定的檔案或目錄是否存在。

GetDirectoryName(ReadOnlySpan<Char>)

傳回指定路徑字串的目錄資訊,此路徑以字元範圍表示。

GetDirectoryName(String)

傳回指定路徑的目錄資訊。

GetExtension(ReadOnlySpan<Char>)

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

GetExtension(String)

指定路徑字串的副檔名 (包括句點 ".")。

GetFileName(ReadOnlySpan<Char>)

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

GetFileName(String)

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

GetFileNameWithoutExtension(ReadOnlySpan<Char>)

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

GetFileNameWithoutExtension(String)

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

GetFullPath(String)

傳回指定路徑字串的絕對路徑。

GetFullPath(String, String)

從相對路徑與完整基底路徑傳回絕對路徑。

GetInvalidFileNameChars()

取得陣列,該陣列包含檔案名稱中不允許的字元。

GetInvalidPathChars()

取得陣列,該陣列包含路徑名稱中不允許的字元。

GetPathRoot(ReadOnlySpan<Char>)

從所指定字元範圍中包含的路徑取得根目錄資訊。

GetPathRoot(String)

從指定字串包含的路徑中取得根目錄資訊。

GetRandomFileName()

傳回隨機資料夾名稱或檔案名稱。

GetRelativePath(String, String)

傳回某個路徑到另一個路徑的相對路徑。

GetTempFileName()

在磁碟上建立具名之零位元組的唯一暫存檔案,然後傳回該檔案的完整路徑。

GetTempPath()

傳回目前使用者的暫存資料夾路徑。

HasExtension(ReadOnlySpan<Char>)

判斷指定字元範圍所表示的路徑是否包含副檔名。

HasExtension(String)

判斷路徑是否包括副檔名。

IsPathFullyQualified(ReadOnlySpan<Char>)

傳回值,指出指定字元範圍所表示的檔案路徑是否固定在特定的磁碟機或 UNC 路徑。

IsPathFullyQualified(String)

傳回值,指出指定的檔案路徑是否固定在特定磁碟機或 UNC 路徑。

IsPathRooted(ReadOnlySpan<Char>)

傳回值,指出所指定字元範圍所表示的檔案路徑是否包含根目錄。

IsPathRooted(String)

傳回值,指出指定的路徑字串是否包含根目錄。

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

將兩個路徑元件串連成單一路徑。

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

將三個路徑元件串連成單一路徑。

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

將四個路徑元件串連成單一路徑。

Join(String, String)

將兩個路徑串連成單一路徑。

Join(String, String, String)

將三個路徑串連成單一路徑。

Join(String, String, String, String)

將四個路徑串連成單一路徑。

Join(String[])

將路徑陣列串連成單一路徑。

TrimEndingDirectorySeparator(ReadOnlySpan<Char>)

修剪指定路徑根以外的一個尾端目錄分隔符號。

TrimEndingDirectorySeparator(String)

修剪指定路徑根以外的一個尾端目錄分隔符號。

TryJoin(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, Span<Char>, Int32)

嘗試將三個路徑元件串連至單一預先配置字元範圍,並傳回值,指出作業是否成功。

TryJoin(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Span<Char>, Int32)

嘗試將兩個路徑元件串連成單一預先配置的字元範圍,然後傳回值,指出作業是否成功。

適用於

另請參閱