linux与windows路径字符串中分隔目录级别

目录分隔符

Path.DirectorySeparatorChar 字段
public static readonly char DirectorySeparatorChar
提供平台特定的字符,该字符用于在反映分层文件系统组织的路径字符串中分隔目录级别。
在该字段中存储的字符不能位于 InvalidPathChars 中。AltDirectorySeparatorChar 和 DirectorySeparatorChar 都适用于在路径字符串中分隔目录级别。
该字段的值在 Unix 上为斜杠(“/”),在 Windows 和 Macintosh 操作系统上为反斜杠(“\”)。

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

替换分隔符

Path.AltDirectorySeparatorChar
public static readonly char AltDirectorySeparatorChar
提供平台特定的替换字符,该替换字符用于在反映分层文件系统组织的路径字符串中分隔目录级别。
在此字段中存储的字符不能位于 InvalidPathChars 中。该字段可被设置为与 DirectorySeparatorChar 相同的值。AltDirectorySeparatorChar 和 DirectorySeparatorChar 都适用于在路径字符串中分隔目录级别。
该字段的值在 Unix 上为反斜杠(“\”),在 Windows 和 Macintosh 操作系统上为斜杠(“/”)。

环境变量中路径分隔符

Path.PathSeparator
public static readonly char PathSeparator
用于在环境变量中分隔路径字符串的平台特定的分隔符。在基于 Windows 的桌面平台上,默认情况下该字段的值是分号 (😉,但在其他平台上可能会有所不同。

卷分隔符

Path.AltDirectorySeparatorChar
public static readonly char VolumeSeparatorChar
提供平台特定的卷分隔符。该字段的值在 Windows 和 Macintosh 上为冒号(“:”),在 Unix 操作系统上为斜杠(“/”)。这对于分析像“c:\windows”或“MacVolume:System Folder”这样的路径最为有用。

//引入命名空间
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
posted @ 2021-03-30 10:31  【唐】三三  阅读(776)  评论(0编辑  收藏  举报