How do I find out what directory my console app is running in with C#?
To get the directory where the .exe file is:
AppDomain.CurrentDomain.BaseDirectory
To get the current directory:
Environment.CurrentDirectory
Application.StartUpPath;
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).
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
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\
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)