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 システムで認識される唯一のディレクトリ区切り文字であり、例からの出力が示すように であり、Windows では です AltDirectorySeparatorChar

  • 実行時にパス区切り文字を動的に取得し、ファイル システム パスに組み込むには、文字列連結を使用します。 たとえば、オブジェクトに適用された

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

    Windows および Unix ベースの両方の AltDirectorySeparatorChar システムで同じであるため、 プロパティから値を取得することもできます。

  • プロパティを取得するAltDirectorySeparatorChar

アプリケーションがクロスプラットフォームでない場合は、システムに適した区切り記号を使用できます。

適用対象

こちらもご覧ください