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

I am working on a C # console application that should work on both Windows and Linux. It will work on .NET 3.5 and Mono. I would like to be able to check if a directory exists inside the current directory. Since Windows uses a backslash to traverse a directory, and Linux uses a backslash, how can I check if a directory exists in another directory?

I am using System.IO.Directory.Exists. I think an easy way to do this is to first check the current working folder for "/" or "\" to determine which one to use, however in some cases there may be an escape character that will ruin everything!

+4
source share
2 answers

Windows is pretty tolerant of this. You can use slash and understand.

If you do not want to take risks, this can be confusing (I'm not 100% sure that Windows always understands this), you can use Path.DirectorySeparatorChar

+5
source

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

 var path = Path.Combine( Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "childFolder"); 
+6
source

Source: https://habr.com/ru/post/1485915/


All Articles