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

10

658

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

Screech answered 23/3, 2009 at 17:49 Comment(2)
Shouldn't that be a string literal? @"C:\MyDirectory\MyFile.bat"Mcquiston
Does somebody want to protect this question who has the rights to do so ? 11 similar answers with the last from 2017..Signalize
F
1035

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.

Focal answered 23/3, 2009 at 17:52 Comment(5)
Indeed, but is there a method called GetDirectory? Isn't it GetDirectoryName?Sinter
You can just use DirectoryName instead of Directory.FullPath can you not?Whiten
I was proofing against receiving a relative name. I hadn't spotted that the path will be absolute. I've now got both versions :)Focal
Is there a single solution which works no matter the type of path we have? That is, be it either a relative or an absolute path.Pharyngo
@UlyssesAlves: I believe new FileInfo(path).Directory.FullName should work in either case.Focal
M
72
System.IO.Path.GetDirectoryName(filename)
Margarettamargarette answered 23/3, 2009 at 17:52 Comment(0)
D
26
Path.GetDirectoryName(filename);
Dorri answered 23/3, 2009 at 17:52 Comment(0)
S
24

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.

Saltish answered 23/3, 2009 at 17:53 Comment(0)
S
11

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

MSDN Link

Sinter answered 23/3, 2009 at 17:52 Comment(0)
J
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.

Judaism answered 27/5, 2014 at 21:40 Comment(0)
O
4

You can get the current Application Path using:

string AssemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();
Organo answered 1/7, 2016 at 16:14 Comment(0)
S
2

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));
Skipton answered 1/7, 2015 at 16:19 Comment(0)
J
2

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

Julesjuley answered 28/8, 2017 at 11:49 Comment(0)
D
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();
Dubrovnik answered 17/7, 2018 at 14:44 Comment(1)
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.Stotinka

© 2022 - 2024 — McMap. All rights reserved.