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

C#.NetConsole Application

C# Problem Overview


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

C# Solutions


Solution 1 - C#

To get the directory where the .exe file is:

AppDomain.CurrentDomain.BaseDirectory

To get the current directory:

Environment.CurrentDirectory

Solution 2 - C#

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( 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.

Solution 3 - C#

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

Solution 4 - C#

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).

Solution 5 - C#

Let's say your .Net core console application project name is DataPrep.

Get Project Base Directory:

Console.WriteLine(Environment.CurrentDirectory);

Output: ~DataPrep\bin\Debug\netcoreapp2.2

Get Project .csproj file directory:
string ProjectDirPath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\..\"));
Console.WriteLine(ProjectDirPath);

Output: ~DataPrep\

Solution 6 - C#

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)

Solution 7 - C#

Application.StartUpPath;

Solution 8 - C#

Use AppContext.BaseDirectory for .net5.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn SheehanView Question on Stackoverflow
Solution 1 - C#HallgrimView Answer on Stackoverflow
Solution 2 - C#Atif AzizView Answer on Stackoverflow
Solution 3 - C#410View Answer on Stackoverflow
Solution 4 - C#Travis IlligView Answer on Stackoverflow
Solution 5 - C#R M Shahidul Islam ShahedView Answer on Stackoverflow
Solution 6 - C#Jakub KotrlaView Answer on Stackoverflow
Solution 7 - C#RickeyView Answer on Stackoverflow
Solution 8 - C#vivek nunaView Answer on Stackoverflow