642

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

bluish
  • 26,356
  • 27
  • 122
  • 180
Even Mien
  • 44,393
  • 43
  • 115
  • 119
  • 3
    Shouldn't that be a string literal? `@"C:\MyDirectory\MyFile.bat"` – Edgar Feb 05 '14 at 12:13
  • 3
    Does somebody want to protect this question who has the rights to do so ? 11 similar answers with the last from 2017.. – Boern May 24 '18 at 14:38

10 Answers10

1011

If you definitely have an absolute path, use Path.GetDirectoryName(path).

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

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

Arad Alvand
  • 8,607
  • 10
  • 51
  • 71
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
71
System.IO.Path.GetDirectoryName(filename)
Cherian
  • 19,107
  • 12
  • 55
  • 69
26
Path.GetDirectoryName(filename);
Grzenio
  • 35,875
  • 47
  • 158
  • 240
22

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.

Aurumaker72
  • 33
  • 1
  • 7
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 3
    there is no 'GetDirectory method in the Path Class; you must have meant 'GetDirectoryName – BillW Aug 03 '16 at 05:41
11

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

MSDN Link

bluish
  • 26,356
  • 27
  • 122
  • 180
Brandon
  • 68,708
  • 30
  • 194
  • 223
5

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.

Derek W
  • 9,708
  • 5
  • 58
  • 67
5

You can get the current Application Path using:

string AssemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
David Castro
  • 1,773
  • 21
  • 21
1

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));
Umut D.
  • 1,746
  • 23
  • 24
1

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

Minh Nguyen
  • 2,106
  • 1
  • 28
  • 34
0

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();
Amir Hajiha
  • 836
  • 8
  • 20
  • 2
    The OP needs `"C:\MyDirectory"` and not `MyDirectory`. The advice to use string manipulation methods is risky, there are many traps, rather use dedicated `Path` methods. – Sinatr Nov 13 '18 at 12:45