按一下以給予評分及指教
MSDN
MSDN Library
.NET 開發
.NET Framework
Path 類別

  開啟低頻寬檢視
本頁僅適用於
Microsoft Visual Studio 2008/.NET Framework 3.5

其他版本也適用於下列軟體:
.NET Framework 類別庫
Path 類別

更新:2007 年 11 月

執行含有檔案或目錄路徑資訊的 String 執行個體 (Instance) 上的作業。這些作業是以跨平台方式來執行的。

命名空間:  System.IO
組件:  mscorlib (在 mscorlib.dll 中)
Visual Basic (宣告)
<ComVisibleAttribute(True)> _
Public NotInheritable Class Path
Visual Basic (使用方式)
您不需宣告靜態類別的執行個體,就能存取其成員。
C#
[ComVisibleAttribute(true)]
public static class Path
Visual C++
[ComVisibleAttribute(true)]
public ref class Path abstract sealed
J#
/** @attribute ComVisibleAttribute(true) */
public final class Path
JScript
public final class Path

.NET Framework 不支援透過本身為裝置名稱的路徑對實體磁碟直接存取,例如 "\\.\PHYSICALDRIVE0"。

路徑為提供檔案或目錄的位置的字串。路徑不必然指向磁碟的位置;例如,路徑可能對應於記憶體中或裝置上的位置。路徑的正確格式由目前平台來決定。例如,在某些系統上,路徑可能以磁碟機或磁碟區字母開始,而這個項目卻不存在於其他系統。在某些系統上,檔案路徑可以含有副檔名,它指示儲存於檔案的資訊類型。副檔名格式是依平台而定的;例如,某些系統將副檔名限制為三個字元,而其他則不然。目前平台也決定用來分隔路徑項目的字元集,和指定路徑時不能使用的字元集。因為這些差異,Path 類別的欄位以及 Path 類別的一些成員的確實行為是依平台而定的。

路徑可以包含絕對或相對位置資訊。絕對路徑完整指定位置:可以唯一識別檔案或目錄,不論目前位置為何。相對路徑指定部分位置:目前位置被當做找出相對路徑指定的檔案時所用的啟始點。若要決定目前目錄,請呼叫 Directory..::.GetCurrentDirectory

Path 類別的大部分成員不會與檔案系統互動,並且不驗證路徑字串所指定檔案的存在。會修改路徑字串的 Path 類別成員 (例如 ChangeExtension) 對檔案系統中檔案的名稱沒有影響。然而,Path 成員的確會驗證指定路徑字串的內容,並擲回 ArgumentException,如果字串含有在路徑字串中不為有效 (如 InvalidPathChars 中所定義) 的字元。例如,在 Windows 架構桌面平台上,無效路徑字元可能包括引號 (")、小於 (&lt;)、大於 (&gt;)、管道 (|)、退格鍵 (\b)、Null (\0),以及 Unicode 字元 16 至 18 和 20 至 25。

Path 類別的成員可以讓您迅速並輕鬆執行一般作業,例如判斷副檔名是否為路徑的一部分,和組合兩個字串成為一個路徑名稱。

Path 類別的所有成員都是靜態的 (Static),因此可以在沒有路徑執行個體的情況下呼叫。

3bdzys9w.alert_note(zh-tw,VS.90).gif注意事項:

在接受路徑做為輸入字串的成員中,那路徑必須是語式正確 (Well-Formed) 的,否則會引發例外狀況。例如,如果路徑為完整限定的,但開頭有空格,路徑並不會在類別的方法中被修剪。因此,路徑若是格式不正確,就會引發例外狀況。同樣的,路徑或路徑的組合不可以做兩次完整限定。例如,"c:\temp c:\windows" 在多數情況下也會引發例外狀況。使用會接受路徑字串的方法時,請確保您的路徑是語式正確的。

在接受路徑的成員中,路徑可以參考檔案或者只參考目錄。指定的路徑也可以參考伺服器和共用名稱的相對路徑或通用命名慣例 (Universal Naming Convention,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 工作

下列程式碼範例示範 Path 類別的一些主要成員。

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

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.
         */
    }
}

Visual C++
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 );
   }
}


J#
import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        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.):");
        char c = ' ';
        for (int iCtr = 0; iCtr < Path.InvalidPathChars.get_Length(); iCtr++) {
            c = Path.InvalidPathChars[iCtr];
            Console.WriteLine(c);
        }
    } //main
} //Test

System..::.Object
  System.IO..::.Path
這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求

.NET Framework

支援版本:3.5、3.0、2.0、1.1、1.0

.NET Compact Framework

支援版本:3.5、2.0、1.0

XNA Framework

支援版本:2.0、1.0
社群內容   什麼是社群內容?
新增內容 RSS  註解
Processing
© 2009 Microsoft Corporation. 著作權所有,並保留一切權利。 使用規定  |  商標  |  隱私權聲明
Page view tracker