Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to get the executing exe's path in .NET?

From program a.exe located in c:/dir I need to open text file c:/dir/text.txt. I don't know where a.exe could be located, but text.txt will always be in the same path. How to get the name of the currently executing assembly from within to program itself so that i can access the text file?

EDIT: what if a.exe is a Windows service? It doesn't have Application as it is not a Windows Applicaion.

Thanks in advance.

like image 976
pistacchio Avatar asked Aug 03 '09 12:08

pistacchio


People also ask

How to get the executable path in c#?

Get Executable Path With the Assembly Class in C#GetEntryAssembly() function is used to get the assembly of the currently executing code. We can get the path of the currently executing code with the Assembly. GetEntryAssembly(). Location property which returns the executable path of the current in a string variable.

What is EXE file in C#?

EXE file is a executable file which runs in a separate process which is managed by OS. 2)DLLs are not directly executable . They are separate files containing functions that can be called by programs and other DLLs to perform computations and functions. An EXE is a program that can be executed . Ex :Windows program.

What is application StartupPath?

StartupPath property returns the path for the executable file that started the application, not the path where the application executable is stored. ExecutablePath property returns the path for the executable file that started the application, including the executable name.


1 Answers

I usually access the directory that contains my application's .exe with:

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); 
like image 109
Tim S. Van Haren Avatar answered Sep 21 '22 05:09

Tim S. Van Haren