SoFunction
Updated on 2025-03-07

How to get the parent path of the current path

How to get the parent path of the current path

//Get the upper directory (parent directory) of the current running path

  topDir = ();
//D:\project\test\test\bin\Debug

//Continue to get the directory of the superior's superior.

string pathto = ;//D:\project\test\test

Get the name of the last folder in the path

string fileName = ("E:\dyl190909\Demo\CulculateTest\Culculate\");
//fileNameThe result isCulculate

Note: A Parent gets the directory up to the next level.

7 ways to get the current path in C#

Code

//Get the full path to the module.string path1 = ().;
//Get and set the fully qualified directory of the current directory (the directory from which the process starts)string path2 = ;
//Get the application's current working directorystring path3 = ();
//Get the base directory of the programstring path4 = ;
//Get and set the name of the directory including the applicationstring path5 = ;
//Get the path to the executable file that started the applicationstring path6 = ;
//Get the path and file name of the executable file that started the applicationstring path7 = ;
StringBuilder str=new StringBuilder();
("().:" + path1);
(":" + path2);
("():" + path3);
(":" + path4);
(":" + path5);
(":" + path6);
(":" + path7);
string allPath = ();

Output result

().:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
():D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\

Usage instructions

1. The () method in Net Framework is used to obtain the current working directory of the application.

If you use this method to obtain the directory where the application is located, you should note that the directory path obtained by the () method changes with the directory determined by the OpenFileDialog, SaveFileDialog and other objects (switch the working directory).

The value obtained by this method will be changed every time a folder is opened or a file is viewed using Explorer.

2. You can get the directory where the application runs, which does not change with the folder you open.

It is only related to the application running directory, and its value is equal to the root directory where the application is started.

For example, if you install the program and are in the C:\Program Files\Program folder, then it is the value.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.