How to navigate a few folders up?

developer747 picture developer747 · Feb 15, 2013 · Viewed 135.8k times · Source

One option would be to do System.IO.Directory.GetParent() a few times. Is there a more graceful way of travelling a few folders up from where the executing assembly resides?

What I trying to do is to find a text file that resides one folder above the application folder. But the assembly itself is inside the bin, which is a few folders deep in the application folder.

Answer

A-Sharabiani picture A-Sharabiani · Jun 17, 2015

Other simple way is to do this:

string path = @"C:\Folder1\Folder2\Folder3\Folder4";
string newPath = Path.GetFullPath(Path.Combine(path, @"..\..\"));

Note This goes two levels up. The result would be: newPath = @"C:\Folder1\Folder2\";