Local directory path

Pretty simple question; How to find the path to the local directory where my exe is located? As-in, I have a .exe, and in the program I need to create a txt file in the directory where exe is! A.

[language - C #]

So, if exe is in C: / Temp and starts from there; my txt should be created in C: / Temp

If the user wants to move exe to D: / Temp and from there from there; I would have to create a txt file in D: / Temp

I tried Directory.GetCurrentDirectory (), but it returns the program execution directory!

+6
c #
source share
5 answers

Assembly.GetExecutingAssembly().Location

+15
source share

try it

 sPath = System.AppDomain.CurrentDomain.BaseDirectory; 

or more

 sAppPath = Environment.CurrentDirectory; 
+6
source share

Similar information is in System.Appdomain.BaseDirectory , the base directory that uses the assembly converter for assemblies for assemblies. In simple cases, this will indicate the location of the source .exe assembly.

 String path = AppDomain.CurrentDomain.BaseDirectory; 
+1
source share

You can try the following:

 this.GetType().Assembly.CodeBase 

or if this is a WinForms application

 Application.ExecutablePath 
0
source share

You can use Application.StartupPath . It gets the path to the executable that launched the application, not including the name of the executable.

0
source share

All Articles