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!
Use Path.Combine
to build your paths. Consider this code:
var path = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"childFolder");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With