Wild Goat
Wild Goat

Reputation: 3589

How do I find the parent directory of a path?

How do I get a parent of a directory, for example:

string upDir = GetOneLvlUp(@"C:\AAA\BBB\CCC\DDD\");

Output:  C:\AAA\BBB\CCC\

Upvotes: 24

Views: 40945

Answers (4)

Sergej Loos
Sergej Loos

Reputation: 312

var upDir = new DirectoryInfo(yourPath).Parent.FullName;

Upvotes: 0

VIKAS
VIKAS

Reputation: 61

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string parentDir = Directory.GetParent(path).FullName;

Upvotes: 6

Saeed Amiri
Saeed Amiri

Reputation: 22565

upDir = Directory.GetParent(path).FullName;

Upvotes: 45

Matt Roberts
Matt Roberts

Reputation: 26917

Everything you want is in the Directory class:

http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

In particular, GetParent:

http://msdn.microsoft.com/en-us/library/system.io.directory.getparent.aspx

Upvotes: 7

Related Questions