tags:

views:

812

answers:

9

How do I find out what directory my console app is running in with C#?

A: 

You will need to specify what language your app is written in.

Sparr
The question is in the .net category, so I suppose he means C#
Yaba
I figured it was going to be a framework class that didn't really matter what language I was using
John Sheehan
+2  A: 

On windows (not sure about Unix etc.) it is the first argument in commandline.

In C/C++ firts item in argv*

WinAPI - GetModuleFileName(NULL, char*, MAX_PATH)

Jakub Kotrla
Yes, it's the same in .NET, too. First arg is always the full path of the executable.
Adam Neal
+18  A: 

To get the directory where the .exe file is:

AppDomain.CurrentDomain.BaseDirectory

To get the current directory:

Environment.CurrentDirectory
Hallgrim
A: 

Check this out.

Iulian Şerbănoiu
+3  A: 

In .NET, you can use System.Environment.CurrentDirectory to get the directory from which the process was started. System.Reflection.Assembly.GetExecutingAssembly().Location will tell you the location of the currently executing assembly (that's only interesting if the currently executing assembly is loaded from somewhere different than the location of the assembly where the process started).

Travis Illig
+4  A: 

Depending on the rights granted to your application, whether shadow copying is in effect or not and other invocation and deployment options, different methods may work or yield different results so you will have to choose your weapon wisely. Having said that, all of the following will yield the same result for a fully-trusted console application that is executed locally at the machine where it resides:

Console.WriteLine( Assembly.GetEntryAssembly().Location );
Console.WriteLine( new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath );
Console.WriteLine( Assembly.GetEntryAssembly().Location );
Console.WriteLine( Environment.GetCommandLineArgs()[0] );
Console.WriteLine( Process.GetCurrentProcess().MainModule.FileName );

You will need to consult the documentation of the above members to see the exact permissions needed.

Atif Aziz
A: 

Application.StartUpPath;

care to link to some documentation?
John Sheehan
A: 

Thank you Hallgrim.

A: 

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

PhantomTypist