How should I handle Windows / Linux paths in C #

My intention is for my application to run on windows and linux.
The application will use a specific directory structure, for example.

appdir/  
      /images
      /sounds

What would be a good way to handle the differences in file names (paths) between Windows and Linux? I do not want to code variables for each platform. e.g. pseudo code

if #Win32
  string pathVar = ':c\somepath\somefile.ext';
else 
  string pathVar = '/somepath/somefile.ext';
+5
source share
3 answers

You can use a constant Path.DirectorySeparatorCharacterthat will be either \or /.

Alternatively, create paths using Path.Combine, which automatically inserts the correct delimiter.

+13
source

Mono. System.IO.Path :

Path.AltDirectorySeparatorChar
Path.DirectorySeparatorChar

Hope this helps!

0
source

All Articles