Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the parent directory in C#?

I use this code for finding the debug directory

public string str_directory = Environment.CurrentDirectory.ToString(); 

"C:\\Users\\Masoud\\Documents\\Visual Studio 2008\\Projects\\MyProj\\MyProj\\bin\\Debug"

How can I find the parent folder as shown below?

"C:\\Users\\Masoud\\Documents\\Visual Studio 2008\\Projects\\MyProj\\MyProj"

like image 914
Masoud Abasian Avatar asked Jul 29 '11 16:07

Masoud Abasian


People also ask

What is the parent directory of a file?

With a directory, a parent directory is a directory containing the current directory. For example, in the MS-DOS path below, the "Windows" directory is the parent directory of the "System32" directory, and C:\ is the root directory.

How do I get parent directory in terminal?

The .. means “the parent directory” of your current directory, so you can use cd .. to go back (or up) one directory. cd ~ (the tilde). The ~ means the home directory, so this command will always change back to your home directory (the default directory in which the Terminal opens).

What is the symbol for parent directory?

.. (double-dot) is the parent of your current directory. ~ (tilde) is your home directory. / (slash) if it present at first character, it usually is called root directory.


2 Answers

You can use System.IO.Directory.GetParent() to retrieve the parent directory of a given directory.

like image 151
Sylence Avatar answered Oct 13 '22 07:10

Sylence


string parent = System.IO.Directory.GetParent(str_directory).FullName; 

See BOL

like image 34
billinkc Avatar answered Oct 13 '22 08:10

billinkc