SoFunction
Updated on 2025-04-10

C# implements the previous directory based on the current directory

In fact, it is very simple and brainless, but it is very practical. It is the method of using the string disassembly:

/// <summary>
    /// Get the root path of the project    /// </summary>
    /// <returns></returns>
    public string GetProjectRootPath()
    {
      string rootPath = "";
      string BaseDirectoryPath = ; // F:\project\WPF\AstroATE-PDR\04. Program\01. Source Code\AstroATE\AstroATE\bin\Debug      // Rewind up to level three to get the required directory      rootPath = (0, ("\\")); // The first \ is an escape character, so you have to write two      rootPath = (0, (@"\"));  // Or write it in this format      rootPath = (0, ("\\")); // @"F:\project\WPF\AstroATE-PDR\04. Program\01. Source Code\AstroATE\AstroATE      return rootPath;
    }

Call this function:

string str = GetProjectRootPath() + @"\data\help document.pdf";  //Find the file you need to find

OK, that's the solution.

Supplement: C# How to get the upper and upper directory of the executable file path

The first type:

DirectoryInfo di = new DirectoryInfo((@"{0}..\..\", )); 

..\There are a few layers that are going back

The second type:

DirectoryInfo info = new DirectoryInfo(); 
String path = ;

The third type:

string WantedPath = (0,(@"\"));

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.