Path Path Path Path Class

定義

在含有檔案或目錄路徑資訊的 String 執行個體上執行作業。Performs operations on String instances that contain file or directory path information. 這些作業是以跨平台方式來執行的。These operations are performed in a cross-platform manner.

public ref class Path abstract sealed
[System.Runtime.InteropServices.ComVisible(true)]
public static class Path
type Path = class
Public Class Path
繼承
PathPathPathPath
屬性

範例

下列範例示範一些主要成員的Path類別。The following example demonstrates some of the main members of the Path class.

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

備註

路徑會是字串,提供檔案或目錄的位置。A path is a string that provides the location of a file or directory. 路徑不會不一定會指向磁碟; 上的位置例如,路徑可能會對應到記憶體中或在裝置上的位置。A path does not necessarily point to a location on disk; for example, a path might map to a location in memory or on a device. 確切路徑的格式取決於目前的平台。The exact format of a path is determined by the current platform. 比方說,在某些系統上,路徑可以使用啟動磁碟機或磁碟區的代號,雖然這個項目不存在於其他系統。For example, on some systems, a path can start with a drive or volume letter, while this element is not present in other systems. 在某些系統中,檔案路徑可以包含延伸模組,這表示儲存在檔案中的資訊類型。On some systems, file paths can contain extensions, which indicate the type of information stored in the file. 副檔名的檔案名稱的格式是平台相依;例如,某些系統限制為三個字元,延伸模組,部分則否。The format of a file name extension is platform-dependent; for example, some systems limit extensions to three characters, and others do not. 目前的平台也會決定一組字元用來分隔項目路徑,以及指定路徑時,無法使用的字元集。The current platform also determines the set of characters used to separate the elements of a path, and the set of characters that cannot be used when specifying paths. 由於這些差異的欄位Path類別的某些成員的確切行為及Path類別具有平台相依性。Because of these differences, the fields of the Path class as well as the exact behavior of some members of the Path class are platform-dependent.

路徑可以包含絕對或相對的位置資訊。A path can contain absolute or relative location information. 絕對路徑完整指定的位置: 檔案或目錄可以唯一識別不論目前的位置。Absolute paths fully specify a location: the file or directory can be uniquely identified regardless of the current location. 相對路徑指定的部分的位置: 尋找指定的相對路徑的檔案時,將會使用做為起點的目前位置。Relative paths specify a partial location: the current location is used as the starting point when locating a file specified with a relative path. 若要判斷目前的目錄,請呼叫Directory.GetCurrentDirectoryTo determine the current directory, call Directory.GetCurrentDirectory.

.NET core 1.1 和更新版本以及.NET Framework 4.6.2 和更新版本也支援的裝置名稱,例如檔案系統物件的存取權 」\? \C:"。.NET Core 1.1 and later versions and .NET Framework 4.6.2 and later versions also support access to file system objects that are device names, such as "\?\C:".

如需有關在 Windows 上的檔案路徑格式的詳細資訊,請參閱 < 在 Windows 系統上的檔案路徑格式For more information on file path formats on Windows, see File path formats on Windows systems.

大部分成員Path類別不會與檔案系統互動,並且不會驗證指定的路徑字串的檔案是否存在。Most members of the Path class do not interact with the file system and do not verify the existence of the file specified by a path string. Path 類別成員,修改路徑字串,例如ChangeExtension,檔案系統中有不會影響檔案的名稱。Path class members that modify a path string, such as ChangeExtension, have no effect on names of files in the file system. Path 成員執行,不過,驗證指定的路徑字串的內容,則擲回ArgumentException例外狀況,如果字串包含無效的路徑字串中從傳回的字元中所定義的字元GetInvalidPathChars方法。Path members do, however, validate the contents of a specified path string, and throw an ArgumentException exception if the string contains characters that are not valid in path strings, as defined in the characters returned from the GetInvalidPathChars method. 例如,在 Windows 架構桌面平台上無效路徑字元可能會包含引號 (")、 小於 (<)、 大於 (>)、 管道 (|)、 退格鍵 (\b)、 null (\0),並透過 18 和 20 到 25 的 Unicode 字元 16。For example, on Windows-based desktop platforms, invalid path characters might include quote ("), less than (<), greater than (>), pipe (|), backspace (\b), null (\0), and Unicode characters 16 through 18 and 20 through 25.

成員Path類別可讓您快速且輕鬆地執行一般的作業,例如判斷副檔名是否為組件的路徑,以及將兩個字串結合成一個路徑名稱。The members of the Path class enable you to quickly and easily perform common operations such as determining whether a file name extension is part of a path, and combining two strings into one path name.

所有成員Path類別都是靜態的而且可以因此呼叫而不需要路徑的執行個體。All members of the Path class are static and can therefore be called without having an instance of a path.

注意

接受的路徑做為輸入字串的成員,該路徑必須是語式正確,或引發例外狀況。In members that accept a path as an input string, that path must be well-formed or an exception is raised. 比方說,如果路徑是完整名稱,但以空格開頭,路徑不被修剪中類別的方法。For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. 因此,路徑的格式不正確,而且會引發例外狀況。Therefore, the path is malformed and an exception is raised. 同樣地,路徑的組合無法完整限定兩次。Similarly, a path or a combination of paths cannot be fully qualified twice. 例如,"c:\temp c:\windows,"也會引發例外狀況在大部分情況下。For example, "c:\temp c:\windows" also raises an exception in most cases. 請確定您的路徑是語式正確的當您使用接受路徑字串的方法。Ensure that your paths are well-formed when using methods that accept a path string.

在接受路徑的成員,路徑可以參考到檔案或只是一個目錄。In members that accept a path, the path can refer to a file or just a directory. 指定的路徑也可以參考相對路徑或通用命名慣例 (UNC) 路徑的伺服器和共用的名稱。The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. 例如,如果以下所有狀況可接受的路徑:For example, all the following are acceptable paths:

  • "c:\\MyDir\\MyFile.txt 」 以 C# 或 Visual Basic 中的 「 c:\MyDir\MyFile.txt"。"c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.

  • "c:\\MyDir 」 以 C# 或 Visual Basic 中的 「 c:\mydir"。"c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

  • "MyDir\\MySubdir 」 以 C# 或 Visual Basic 中的 「 MyDir\MySubDir"。"MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

  • 「\\\\MyServer\\MyShare 」 在 C# 中,或 「\\MyServer\MyShare"在 Visual Basic 中。"\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

因為所有這些作業會在字串上執行,就無法驗證結果在所有案例中有效。Because all these operations are performed on strings, it is impossible to verify that the results are valid in all scenarios. 比方說,GetExtension方法會剖析字串,您將傳遞給它,並從該字串會傳回延伸模組。For example, the GetExtension method parses a string that you pass to it and returns the extension from that string. 不過,這不表示該副檔名的檔案存在磁碟上。However, this does not mean that a file with that extension exists on the disk.

如需一般 I/O 工作的清單,請參閱 < 一般 I/O 工作For a list of common I/O tasks, see Common I/O Tasks.

欄位

AltDirectorySeparatorChar AltDirectorySeparatorChar AltDirectorySeparatorChar AltDirectorySeparatorChar

提供平台特定替代字元,用以在反映階層式檔案系統組織的路徑字串中來分隔目錄層級。Provides a platform-specific alternate character used to separate directory levels in a path string that reflects a hierarchical file system organization.

DirectorySeparatorChar DirectorySeparatorChar DirectorySeparatorChar DirectorySeparatorChar

提供平台特定字元,用以在反映階層式檔案系統組織的路徑字串中分隔目錄層級。Provides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization.

InvalidPathChars InvalidPathChars InvalidPathChars InvalidPathChars

提供字元的平台特定陣列,這些字元不可以在傳遞給 Path 類別成員的路徑字串引數中指定。Provides a platform-specific array of characters that cannot be specified in path string arguments passed to members of the Path class.

PathSeparator PathSeparator PathSeparator PathSeparator

平台特定分隔符號字元,用來分隔環境變數中的路徑字串。A platform-specific separator character used to separate path strings in environment variables.

VolumeSeparatorChar VolumeSeparatorChar VolumeSeparatorChar VolumeSeparatorChar

提供平台特定磁碟區分隔符號字元。Provides a platform-specific volume separator character.

方法

ChangeExtension(String, String) ChangeExtension(String, String) ChangeExtension(String, String) ChangeExtension(String, String)

變更路徑字串的副檔名。Changes the extension of a path string.

Combine(String[]) Combine(String[]) Combine(String[]) Combine(String[])

將一個字串陣列合併為單一路徑。Combines an array of strings into a path.

Combine(String, String) Combine(String, String) Combine(String, String) Combine(String, String)

將兩個字串合併為一個路徑。Combines two strings into a path.

Combine(String, String, String) Combine(String, String, String) Combine(String, String, String) Combine(String, String, String)

將三個字串合併為一個路徑。Combines three strings into a path.

Combine(String, String, String, String) Combine(String, String, String, String) Combine(String, String, String, String) Combine(String, String, String, String)

將四個字串合併為一個路徑。Combines four strings into a path.

GetDirectoryName(ReadOnlySpan<Char>) GetDirectoryName(ReadOnlySpan<Char>) GetDirectoryName(ReadOnlySpan<Char>) GetDirectoryName(ReadOnlySpan<Char>)

傳回指定路徑字串的目錄資訊,此路徑以字元範圍表示。Returns the directory information for the specified path represented by a character span.

GetDirectoryName(String) GetDirectoryName(String) GetDirectoryName(String) GetDirectoryName(String)

傳回指定路徑字串的目錄資訊。Returns the directory information for the specified path string.

GetExtension(String) GetExtension(String) GetExtension(String) GetExtension(String)

傳回指定路徑字串的副檔名。Returns the extension of the specified path string.

GetExtension(ReadOnlySpan<Char>) GetExtension(ReadOnlySpan<Char>) GetExtension(ReadOnlySpan<Char>) GetExtension(ReadOnlySpan<Char>)

傳回檔案路徑的副檔名,此路徑以唯讀的字元範圍表示。Returns the extension of a file path that is represented by a read-only character span.

GetFileName(ReadOnlySpan<Char>) GetFileName(ReadOnlySpan<Char>) GetFileName(ReadOnlySpan<Char>) GetFileName(ReadOnlySpan<Char>)

傳回檔案路徑的檔案名稱和副檔名,此路徑以唯讀的字元範圍表示。Returns the file name and extension of a file path that is represented by a read-only character span.

GetFileName(String) GetFileName(String) GetFileName(String) GetFileName(String)

傳回指定路徑字串的檔案名稱和副檔名。Returns the file name and extension of the specified path string.

GetFileNameWithoutExtension(ReadOnlySpan<Char>) GetFileNameWithoutExtension(ReadOnlySpan<Char>) GetFileNameWithoutExtension(ReadOnlySpan<Char>) GetFileNameWithoutExtension(ReadOnlySpan<Char>)

傳回檔案路徑的檔案名稱和副檔名,此路徑以唯讀的字元範圍表示。Returns the file name without the extension of a file path that is represented by a read-only character span.

GetFileNameWithoutExtension(String) GetFileNameWithoutExtension(String) GetFileNameWithoutExtension(String) GetFileNameWithoutExtension(String)

傳回沒有副檔名的指定路徑字串的檔案名稱。Returns the file name of the specified path string without the extension.

GetFullPath(String) GetFullPath(String) GetFullPath(String) GetFullPath(String)

傳回指定路徑字串的絕對路徑。Returns the absolute path for the specified path string.

GetFullPath(String, String) GetFullPath(String, String) GetFullPath(String, String) GetFullPath(String, String)

從完整的基底路徑和相對路徑傳回絕對路徑。Returns an absolute path from a fully qualified base path and a relative path.

GetInvalidFileNameChars() GetInvalidFileNameChars() GetInvalidFileNameChars() GetInvalidFileNameChars()

取得陣列,該陣列包含檔案名稱中不允許的字元。Gets an array containing the characters that are not allowed in file names.

GetInvalidPathChars() GetInvalidPathChars() GetInvalidPathChars() GetInvalidPathChars()

取得陣列,該陣列包含路徑名稱中不允許的字元。Gets an array containing the characters that are not allowed in path names.

GetPathRoot(String) GetPathRoot(String) GetPathRoot(String) GetPathRoot(String)

取得指定路徑的根目錄資訊。Gets the root directory information of the specified path.

GetPathRoot(ReadOnlySpan<Char>) GetPathRoot(ReadOnlySpan<Char>) GetPathRoot(ReadOnlySpan<Char>) GetPathRoot(ReadOnlySpan<Char>)

從所指定字元範圍中包含的路徑取得根目錄資訊。Gets the root directory information from the path contained in the specified character span.

GetRandomFileName() GetRandomFileName() GetRandomFileName() GetRandomFileName()

傳回隨機資料夾名稱或檔案名稱。Returns a random folder name or file name.

GetRelativePath(String, String) GetRelativePath(String, String) GetRelativePath(String, String) GetRelativePath(String, String)

傳回某個路徑到另一個路徑的相對路徑。Returns a relative path from one path to another.

GetTempFileName() GetTempFileName() GetTempFileName() GetTempFileName()

在磁碟上建立具名之零位元組的唯一暫存檔案,然後傳回該檔案的完整路徑。Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.

GetTempPath() GetTempPath() GetTempPath() GetTempPath()

傳回目前使用者的暫存資料夾路徑。Returns the path of the current user's temporary folder.

HasExtension(ReadOnlySpan<Char>) HasExtension(ReadOnlySpan<Char>) HasExtension(ReadOnlySpan<Char>) HasExtension(ReadOnlySpan<Char>)

判斷指定字元範圍所表示的路徑是否包含副檔名。Determines whether the path represented by the specified character span includes a file name extension.

HasExtension(String) HasExtension(String) HasExtension(String) HasExtension(String)

判斷路徑是否包括副檔名。Determines whether a path includes a file name extension.

IsPathFullyQualified(ReadOnlySpan<Char>) IsPathFullyQualified(ReadOnlySpan<Char>) IsPathFullyQualified(ReadOnlySpan<Char>) IsPathFullyQualified(ReadOnlySpan<Char>)

傳回值,指出指定字元範圍所表示的檔案路徑是否固定在特定的磁碟機或 UNC 路徑。Returns a value that indicates whether the file path represented by the specified character span is fixed to a specific drive or UNC path.

IsPathFullyQualified(String) IsPathFullyQualified(String) IsPathFullyQualified(String) IsPathFullyQualified(String)

傳回值,指出指定的檔案路徑是否固定在特定磁碟機或 UNC 路徑。Returns a value that indicates whether the specified file path is fixed to a specific drive or UNC path.

IsPathRooted(String) IsPathRooted(String) IsPathRooted(String) IsPathRooted(String)

傳回值,指出指定的路徑字串是否包含根目錄。Returns a value indicating whether the specified path string contains a root.

IsPathRooted(ReadOnlySpan<Char>) IsPathRooted(ReadOnlySpan<Char>) IsPathRooted(ReadOnlySpan<Char>) IsPathRooted(ReadOnlySpan<Char>)

傳回值,指出所指定字元範圍所表示的檔案路徑是否包含根目錄。Returns a value that indicates whether the specified character span that represents a file path contains a root.

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

將兩個路徑元件串連成單一路徑。Concatenates two path components into a single path.

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

將三個路徑元件串連成單一路徑。Concatenates three path components into a single path.

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

嘗試將兩個路徑元件串連成單一預先配置的字元範圍,然後傳回值,指出作業是否成功。Attempts to concatenate two path components to a single preallocated character span, and returns a value that indicates whether the operation succeeded.

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

嘗試將兩個路徑元件串連成單一預先配置的字元範圍,然後傳回值,指出作業是否成功。Attempts to concatenate two path components to a single preallocated character span, and returns a value that indicates whether the operation succeeded.

適用於

另請參閱