Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the root directory of the EXE - C# [duplicate]

Tags:

c#

I have the EXE in the following location:

C:\Projects\Bin\Sample.EXE

I want to get the path in C# like this:

C:\Projects\

One way to do this is:

string str = "C:\\Projects\\Bin\\Sample.EXE"
string res = str.Replace("Bin", "")

But this is NOT an efficient way. My Bin folder can be changed to Bin1, Bin2 etc ... So the Bin name is NOT constant. It can be C:\\Projects\\Debug\\Sample.EXE also. Basically, I want to move one level up in the directory structure.

Can you please provide me sample code ?

Here is the sample code I am looking for:

@marc_s code

This is completely different from above two previous questions and I did not find solution to my issue using earlier two links.

like image 581
Kishore Babu. Peddi Avatar asked Jan 08 '23 18:01

Kishore Babu. Peddi


1 Answers

Get the location of the currently executing assembly, and go up one level from that directory - like this:

-- get path of the executing assembly
string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

-- get the parent directory for that path
string parentPath = Path.GetFullPath(Path.Combine(currentPath, ".."));
like image 114
marc_s Avatar answered Jan 15 '23 05:01

marc_s