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


Question

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

1
81
2/19/2009 5:59:37 AM

Accepted Answer

To get the directory where the .exe file is:

AppDomain.CurrentDomain.BaseDirectory

To get the current directory:

Environment.CurrentDirectory
148
9/18/2008 9:45:45 PM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon