System.io.DirectorySeparatorChar returns the character "\" while System.io.AltDirectorySeparatorChar returns "/"
to be more specific what is the job of System.Io.AltDirectorySeparatorChar and when we use it?
System.io.DirectorySeparatorChar returns the character "\" while System.io.AltDirectorySeparatorChar returns "/"
to be more specific what is the job of System.Io.AltDirectorySeparatorChar and when we use it?
From the documentation (https://learn.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.
You can use alt separator in your path strings the same way as you use normal separator. Like this:
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}");
}
}
And the output:
C:\dir\subdir file.txt
C:\dir\subdir file.txt
\dir\subdir file.txt
My understanding is that DirectorySeparatorChar is platform-specific default separator, while AltDirectorySeparatorChar is platform-independent universal separator.
On Windows : DirectorySeparatorChar is backslash, AltDirectorySeparatorChar is forward slash
On Unix-based : Both are forward slash
It is because Windows natively prefers backslash but also supports forward slash, while Unix-based systems only support forward slash. Practically, it means you can use either one.
I would personally prefer DirectorySeparatorChar, because I simply prefer backslash on Windows. But perhaps the best is to use Path.Combine and let the choice up to this method.