Platform specific path separator

The Path class contains platform specific characters

Home DailyDrop

Daily Knowledge Drop

The static Path class contains a number of helpful, platform specific properties (and methods) to assist when working with filenames and file paths. These include:

  • The operating system specific directory separator character
  • The operating system specific alternate directory separator character
  • The operating system specific path separator character
  • The operating system specific volume separator character
  • The operating system specific invalid filename characters
  • The operating system specific invalid path characters

Code snippet

A simple code snippet to demonstrate how to access the various characters:

// All the separator characters are available on the Path static class
Console.WriteLine($"DirectorySeparatorChar: '{Path.DirectorySeparatorChar}'");
Console.WriteLine($"AltDirectorySeparatorChar: '{Path.AltDirectorySeparatorChar}'");
Console.WriteLine($"PathSeparator: '{Path.PathSeparator}'");
Console.WriteLine($"VolumeSeparatorChar: '{Path.VolumeSeparatorChar}'");

// display all the invalid file name characters
Console.WriteLine($"Invalid filename chars:");
foreach(var chr in Path.GetInvalidFileNameChars())
{
    Console.WriteLine($"   {chr}");
}

// display all the invalid path characters
Console.WriteLine($"Invalid path chars:");
foreach (var chr in Path.GetInvalidPathChars())
{
    Console.WriteLine($"   {chr}");
}

Windows output

The output when running the above on a Windows environment - the invalid filename and invalid path characters have been trimmed as the list for Windows is fairly long:

DirectorySeparatorChar: '\'
AltDirectorySeparatorChar: '/'
PathSeparator: ';'
VolumeSeparatorChar: ':'
Invalid filename chars:
   "
   <
   >
   |

   ☺
   ☻
   ♥
   ♦
   ♣
   ♠
// trimmed

Linux output

Running the same code in a Linux environment yields different output - no trimming of the list here, as apparently Linux supports a larger list of characters than Windows:

DirectorySeparatorChar: '/'
AltDirectorySeparatorChar: '/'
PathSeparator: ':'
VolumeSeparatorChar: '/'
Invalid filename chars:
   ?
   /
Invalid path chars:
   ?

Notes

Leveraging the above functionality makes building up and validating file names and file paths much easier, simpler and quicker, and should definitely be preferred over rolling out ones own path builder or filename validator.


References

Path Class in C#

Daily Drop 193: 02-11-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net path separator