共用方式為


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

提供平臺特定的磁碟區分隔符。

方法

名稱 Description
ChangeExtension(String, String)

變更路徑字串的延伸。

Combine(ReadOnlySpan<String>)

將字串範圍結合成路徑。

Combine(String, String, String, String)

將四個字串合併成路徑。

Combine(String, String, String)

將三個字串合併成路徑。

Combine(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, String)

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

GetFullPath(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>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

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

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

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

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

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

Join(ReadOnlySpan<String>)

將路徑範圍串連成單一路徑。

Join(String, String, String, String)

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

Join(String, String, String)

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

Join(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)

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

適用於

另請參閱