views:

9663

answers:

5

I have a console app, and I would like to find the application's path.

In winforms I can use Application.StartupPath to find the current path, but this doesn't seem to be available in a console app?

+1  A: 
Environment.CurrentDirectory
BobbyShaftoe
Reports a different directory if run in the Visual Studio IDE vs Outside of the IDE
JSmyth
Also, there's no requirement that the executable ever resides in the current directory.
Chris Farmer
+14  A: 

System.Reflection.Assembly.GetExecutingAssembly().Location

Combine that with System.IO.Path.GetDirectory if all you want is the directory.

Boo
Thats it, but GetExecutingAssembly is a method so should be System.Reflection.Assembly.GetExecutingAssembly().Location
JSmyth
Whoops, close but .Location includes the name of the executable, I just want the directory.
JSmyth
Ah, I see System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)Does the trick.
JSmyth
:-) .
Boo
+2  A: 

You looking to do this?

System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
PSU_Kardi
+2  A: 

To get the current executing directory you can use

System.IO.Directory.GetCurrentDirectory()
Ollie
+6  A: 

You can use the following code to get the current application directory.

> AppDomain.CurrentDomain.BaseDirectory