You can use the DirectoryInfo object to get the parent chain as much as you need. If you want to get the parent directory at three levels, you can do something like this:
DirectoryInfo di = new DirectoryInfo(@"C:\ProgramFiles\Folder1\Folder2\Folder3\Folder4\file.txt"); for(int i = 0; i<3; i++){ di = di.Parent; }
Obviously, you can change the way you stop your workaround to meet your needs. Once you have the DirectoryInfo object where you need it, you can do what you need. I assume that you will need the full path or use the Directory object. To get the full path, use the FullName property. If, for example, you need all the files in a directory, you can do the following:
string[] fileNames = Directory.GetFiles(di.FullName);
source share