Search code examples
c#directoryfilesystems

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"


Solution

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