DirectorySeparatorChar 和 AltDirectorySeparatorChar 有什么区别

What is the difference between DirectorySeparatorChar and AltDirectorySeparatorChar

System.io.DirectorySeparatorChar returns 字符“\” 而 System.io.AltDirectorySeparatorChar returns “/”

更具体地说,System.Io.AltDirectorySeparatorChar 的工作是什么以及我们何时使用它?

来自文档 (https://docs.microsoft.com/en-us/dotnet/api/system.io.path.altdirectoryseparatorchar)

Provides a platform-specific alternate character used to separate directory levels in a path string that reflects a hierarchical file system organization.

您可以像使用普通分隔符一样在路径字符串中使用 alt 分隔符。像这样:

static void Main(string[] args)
{
        string[] pathStrings =
        {
            @"C:\dir\subdir\file.txt",
            "C:/dir/subdir/file.txt",
            "/dir/subdir/file.txt"
        };

        foreach (var p in pathStrings)
        {
            var dir = Path.GetDirectoryName(p);
            var file = Path.GetFileName(p);
            Console.WriteLine($"{dir} {file}");
        }

}

并且输出:

C:\dir\subdir file.txt
C:\dir\subdir file.txt
\dir\subdir file.txt