What is the simplest way to get the directory that a file is in? I'm using this to set a working directory.
string filename = "C:\MyDirectory\MyFile.bat"
In this example, I should get "C:\MyDirectory".
What is the simplest way to get the directory that a file is in? I'm using this to set a working directory.
string filename = "C:\MyDirectory\MyFile.bat"
In this example, I should get "C:\MyDirectory".
If you're definitely got an absolute path, use Path.GetDirectoryName(path)
.
If you might only get a relative name, use new FileInfo(path).Directory.FullPath
.
You can use System.IO.Path.GetDirectory(filename), or turn the path into a FileInfo, and use fileInfo.Directory.
If you're doing other things with the path, the FileInfo may have advantages.