Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.IO.Directory.Exists() use with both Windows and Linux

Tags:

c#

mono

I am working on a C# console app that needs to be able to run on both Windows and Linux. It will run on .NET 3.5 and Mono. I would like to be able to check to see if a directory exists inside of the currently running directory. Since Windows uses a backslash for directory traversal, and Linux uses a forward slash, how do I check to see if a directory exists within another directory?

I am using System.IO.Directory.Exists. I think a simple way to do this is to first check the current working folder for either "/" or "\" to determine which one to use, however in some instances there may be an escape character which would mess things up!

like image 277
muncherelli Avatar asked Dec 20 '22 04:12

muncherelli


1 Answers

Use Path.Combine to build your paths. Consider this code:

var path = Path.Combine(
    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
    "childFolder");
like image 126
Mike Perrenoud Avatar answered Dec 30 '22 11:12

Mike Perrenoud