Path.DirectorySeparatorChar 欄位

定義

提供平台特定字元,用以在反映階層式檔案系統組織的路徑字串中分隔目錄層級。

public: static initonly char DirectorySeparatorChar;
public static readonly char DirectorySeparatorChar;
 staticval mutable DirectorySeparatorChar : char
Public Shared ReadOnly DirectorySeparatorChar As Char 

欄位值

範例

下列範例會顯示 Path Windows 和 Unix 系統上的域值。 請注意,Windows 支援欄位) 所 AltDirectorySeparatorChar 傳回的正斜線 (,或欄位所傳回 DirectorySeparatorChar 的反斜線 () 做為路徑分隔符號,而 Unix 型系統僅支援正斜線。

using System;
using System.IO;

class Program
{
    static void Main()
    {
        Console.WriteLine($"Path.DirectorySeparatorChar: '{Path.DirectorySeparatorChar}'");
        Console.WriteLine($"Path.AltDirectorySeparatorChar: '{Path.AltDirectorySeparatorChar}'");
        Console.WriteLine($"Path.PathSeparator: '{Path.PathSeparator}'");
        Console.WriteLine($"Path.VolumeSeparatorChar: '{Path.VolumeSeparatorChar}'");
        var invalidChars = Path.GetInvalidPathChars();
        Console.WriteLine($"Path.GetInvalidPathChars:");
        for (int ctr = 0; ctr < invalidChars.Length; ctr++) 
        {
            Console.Write($"  U+{Convert.ToUInt16(invalidChars[ctr]):X4} ");
            if ((ctr + 1) % 10 == 0) Console.WriteLine();
        }
        Console.WriteLine();
    }
}
// The example displays the following output when run on a Windows system:
//    Path.DirectorySeparatorChar: '\'
//    Path.AltDirectorySeparatorChar: '/'
//    Path.PathSeparator: ';'
//    Path.VolumeSeparatorChar: ':'
//    Path.GetInvalidPathChars:
//      U+007C)   U+0000)   U+0001)   U+0002)   U+0003)   U+0004)   U+0005)   U+0006)   U+0007)   U+0008)
//      U+0009)   U+000A)   U+000B)   U+000C)   U+000D)   U+000E)   U+000F)   U+0010)   U+0011)   U+0012)
//      U+0013)   U+0014)   U+0015)   U+0016)   U+0017)   U+0018)   U+0019)   U+001A)   U+001B)   U+001C)
//      U+001D)   U+001E)   U+001F)
//
// The example displays the following output when run on a Linux system:
//    Path.DirectorySeparatorChar: '/'
//    Path.AltDirectorySeparatorChar: '/'
//    Path.PathSeparator: ':'
//    Path.VolumeSeparatorChar: '/'
//    Path.GetInvalidPathChars:
//      U+0000
Imports System.IO

Module Program
    Sub Main()
       Console.WriteLine($"Path.DirectorySeparatorChar: '{Path.DirectorySeparatorChar}'")
        Console.WriteLine($"Path.AltDirectorySeparatorChar: '{Path.AltDirectorySeparatorChar}'")
        Console.WriteLine($"Path.PathSeparator: '{Path.PathSeparator}'")
        Console.WriteLine($"Path.VolumeSeparatorChar: '{Path.VolumeSeparatorChar}'")
        Dim invalidChars = Path.GetInvalidPathChars()
        Console.WriteLine($"Path.GetInvalidPathChars:")
        For ctr As Integer = 0 To invalidChars.Length - 1 
            Console.Write($"  U+{Convert.ToUInt16(invalidChars(ctr)):X4} ")
            if (ctr + 1) Mod 10 = 0 Then Console.WriteLine()
        Next
        Console.WriteLine()
        Console.WriteLine("Hello World!")
    End Sub
End Module
' The example displays the following output when run on a Windows system:
'    Path.DirectorySeparatorChar: '\'
'    Path.AltDirectorySeparatorChar: '/'
'    Path.PathSeparator: ';'
'    Path.VolumeSeparatorChar: ':'
'    Path.GetInvalidPathChars:
'      U+007C)   U+0000)   U+0001)   U+0002)   U+0003)   U+0004)   U+0005)   U+0006)   U+0007)   U+0008)
'      U+0009)   U+000A)   U+000B)   U+000C)   U+000D)   U+000E)   U+000F)   U+0010)   U+0011)   U+0012)
'      U+0013)   U+0014)   U+0015)   U+0016)   U+0017)   U+0018)   U+0019)   U+001A)   U+001B)   U+001C)
'      U+001D)   U+001E)   U+001F)
'
' The example displays the following output when run on a Linux system:
'    Path.DirectorySeparatorChar: '/'
'    Path.AltDirectorySeparatorChar: '/'
'    Path.PathSeparator: ':'
'    Path.VolumeSeparatorChar: '/'
'    Path.GetInvalidPathChars:
'      U+0000

備註

AltDirectorySeparatorCharDirectorySeparatorChar 都有效用來分隔路徑字串中的目錄層級。

當您使用 .NET Core 開發在多個平臺上執行的應用程式時:

  • 如果您想要硬式編碼目錄分隔符號,您應該使用正斜線 () / 字元。 這是 Unix 系統上唯一可辨識的目錄分隔符號,如範例所示的輸出,而且是 AltDirectorySeparatorChar Windows 上的 。

  • 使用字串串連在執行時間動態擷取路徑分隔符號,並將其併入檔案系統路徑中。 例如,套用至物件的

    separator = Path.DirectorySeparatorChar;
    path = $"{separator}users{separator}user1{separator}";
    
    separator = Path.DirectorySeparatorChar
    path = $"{separator}users{separator}user1{separator}"
    

    您也可以從 屬性擷 AltDirectorySeparatorChar 取值,因為它在 Windows 和 Unix 系統上都相同。

  • 擷取 AltDirectorySeparatorChar 屬性

如果您的應用程式不是跨平臺,您可以使用適合您系統的分隔符號。

適用於

另請參閱