How do I get the directory from a file's full path?

C#.NetFileFile IoDirectory

C# Problem Overview


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".

C# Solutions


Solution 1 - C#

If you've definitely got an absolute path, use Path.GetDirectoryName(path).

If you might only get a relative name, use new FileInfo(path).Directory.FullName.

Note that Path and FileInfo are both found in the namespace System.IO.

Solution 2 - C#

System.IO.Path.GetDirectoryName(filename)

Solution 3 - C#

Path.GetDirectoryName(filename);

Solution 4 - C#

You can use System.IO.Path.GetDirectoryName(fileName), or turn the path into a FileInfo using FileInfo.Directory.

If you're doing other things with the path, the FileInfo class may have advantages.

Solution 5 - C#

You can use Path.GetDirectoryName and just pass in the filename.

MSDN Link

Solution 6 - C#

If you are working with a FileInfo object, then there is an easy way to extract a string representation of the directory's full path via the DirectoryName property.

Description of the FileInfo.DirectoryName Property via MSDN:

> Gets a string representing the directory's full path.

Sample usage:

string filename = @"C:\MyDirectory\MyFile.bat";
FileInfo fileInfo = new FileInfo(filename);
string directoryFullPath = fileInfo.DirectoryName; // contains "C:\MyDirectory"

Link to the MSDN documentation.

Solution 7 - C#

You can get the current Application Path using:

string AssemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();

Solution 8 - C#

First, you have to use System.IO namespace. Then;

string filename = @"C:\MyDirectory\MyFile.bat";
string newPath = Path.GetFullPath(fileName);

or

string newPath = Path.GetFullPath(openFileDialog1.FileName));

Solution 9 - C#

You can use Path.GetFullPath for most of the case. But if you want to get the path also in the case of the file name is relatively located then you can use the below generic method:

string GetPath(string filePath)
{
  return Path.GetDirectoryName(Path.GetFullPath(filePath))
}

For example:

GetPath("C:\Temp\Filename.txt") return "C:\Temp\"

GetPath("Filename.txt") return current working directory like "C:\Temp\"

Solution 10 - C#

In my case, I needed to find the directory name of a full path (of a directory) so I simply did:

var dirName = path.Split('\\').Last();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEven MienView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#CherianView Answer on Stackoverflow
Solution 3 - C#GrzenioView Answer on Stackoverflow
Solution 4 - C#Reed CopseyView Answer on Stackoverflow
Solution 5 - C#BrandonView Answer on Stackoverflow
Solution 6 - C#Derek WView Answer on Stackoverflow
Solution 7 - C#David CastroView Answer on Stackoverflow
Solution 8 - C#Umut D.View Answer on Stackoverflow
Solution 9 - C#Minh NguyenView Answer on Stackoverflow
Solution 10 - C#Amir HajihaView Answer on Stackoverflow