How to navigate across multiple folders?

One option is to execute System.IO.Directory.GetParent () several times. Is there a more elegant way to move multiple folders up where the running assembly is?

I am trying to find a text file that is in the same folder above the application folder. But the assembly itself is located inside the garbage container, which is a few folders in the application folder.

+79
c # console-application
Feb 15 '13 at 16:50
source share
9 answers

Another simple way is the following:

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

Note These are two levels up. The result will be: newPath = @"C:\Folder1\Folder2\";

+152
Jun 17 '15 at 21:29
source share

if c: \ folder1 \ folder2 \ folder3 \ bin is the path, the following code will return the base folder of the bin folder path

 string directory=System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString()); 

t, s: \ folder1 \ folder2 \ folder3

if you want the path folder2, then you can get the directory

 string directory = System.IO.Directory.GetParent(System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString(); 

then you will get the path as c: \ folder1 \ folder2 \

+16
Sep 30 '16 at 8:45
source share

You can use ..\path to go up one level, ..\..\path to go two levels up from the path.

You can also use the Path class.

C # Path class

+4
Feb 15 '13 at 16:52
source share

This is what worked best for me:

string parentOfStartupPath = Path.GetFullPath(Path.Combine(Application.StartupPath, @"../"));

Getting the β€œright” path was not a problem, adding β€œ../” obviously does it, but after that this line is not used because it just adds β€œ../” to the end. Surrounding it with Path.GetFullPath() will give you an absolute path, making it usable.

+4
May 21 '14 at 9:54
source share

The following method looks for a file starting with the application launch path (* .exe folder). If the file is not found there, the parent folders are executed until neither the file nor the root folder is found. null returned if the file is not found.

 public static FileInfo FindApplicationFile(string fileName) { string startPath = Path.Combine(Application.StartupPath, fileName); FileInfo file = new FileInfo(startPath); while (!file.Exists) { if (file.Directory.Parent == null) { return null; } DirectoryInfo parentDir = file.Directory.Parent; file = new FileInfo(Path.Combine(parentDir.FullName, file.Name)); } return file; } 

Note. Application.StartupPath commonly used in WinForms applications, but also works in console applications; however, you will need to install a link to the System.Windows.Forms assembly. You can replace Application.StartupPath with
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) , if you prefer.

+3
Feb 11 '14 at 18:26
source share

this can help

 string parentOfStartupPath = Path.GetFullPath(Path.Combine(Application.StartupPath, @"../../")) + "Orders.xml"; if (File.Exists(parentOfStartupPath)) { // file found } 
+1
Jul 20 '15 at 12:35
source share

Maybe you can use a function if you want to declare the number of levels and put it in a function?

 private String GetParents(Int32 noOfLevels, String currentpath) { String path = ""; for(int i=0; i< noOfLevels; i++) { path += @"..\"; } path += currentpath; return path; } 

And you can call it like this:

 String path = this.GetParents(4, currentpath); 
0
Feb 15 '13 at 16:56
source share

If you know the folder you want to navigate to, find its index, and then the substring.

  var ind = Directory.GetCurrentDirectory().ToString().IndexOf("Folderame"); string productFolder = Directory.GetCurrentDirectory().ToString().Substring(0, ind); 
0
May 6 '18 at 19:07
source share

I have several virtual directories and I cannot use directory methods. So, I made a simple split / join function for those interested. Not so safe though.

 var splitResult = filePath.Split(new[] {'/', '\\'}, StringSplitOptions.RemoveEmptyEntries); var newFilePath = Path.Combine(filePath.Take(splitResult.Length - 1).ToArray()); 

So, if you want to raise 4 up, you just need to change 1 to 4 and add some checks to avoid exceptions.

0
Jan 20 '19 at 1:12
source share



All Articles