Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I handle windows/Linux paths in c#

Tags:

c#

path

My intention is for my application to run on windows and linux.
The application will be making use of a certain directory structure e.g.

appdir/  
      /images
      /sounds

What would be a good way of handling the differences in file(path) naming differences between windows and linux? I don't 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';
like image 402
Eminem Avatar asked Apr 22 '11 18:04

Eminem


1 Answers

You can use the Path.DirectorySeparatorChar constant, which will be either \ or /.

Alternatively, create paths using Path.Combine, which will automatically insert the correct separator.

like image 92
SLaks Avatar answered Sep 19 '22 22:09

SLaks