Jayesh
Jayesh

Reputation: 3951

Path of the local directory

A rather simple question; how to find the path of the local directory in which my exe is placed? As-in I have an .exe and in the program I have to create a txt file in the directory where the exe is!

[language - C#]

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

If the user wishes to move the exe to D:/Temp and runs from there; I should be able to create the txt file in D:/Temp

I tried the Directory.GetCurrentDirectory() but that returns the directory of the execution of the program!

Upvotes: 9

Views: 51484

Answers (5)

kbmanjunatha
kbmanjunatha

Reputation: 1

You can use Application.StartupPath. It Gets the path for the executable file that started the application, not including the executable name.

Upvotes: 0

gimel
gimel

Reputation: 86492

A similar information is in System.Appdomain.BaseDirectory, the base directory that the assembly resolver uses to probe for assemblies. In simple cases, this will point to the location of the original .exe assembly.

String path = AppDomain.CurrentDomain.BaseDirectory;

Upvotes: 2

Vimard
Vimard

Reputation: 1259

try this

sPath = System.AppDomain.CurrentDomain.BaseDirectory;

or else

sAppPath = Environment.CurrentDirectory;

Upvotes: 13

Lazarus
Lazarus

Reputation: 43114

You could try this:

this.GetType().Assembly.CodeBase

or if it's a WinForms app

Application.ExecutablePath

Upvotes: 0

Hans Olsson
Hans Olsson

Reputation: 55049

Assembly.GetExecutingAssembly().Location

Upvotes: 18

Related Questions