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

前端 未结 8 536
醉话见心
醉话见心 2020-12-07 12:55

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

相关标签:
8条回答
  • 2020-12-07 13:35

    To get the directory where the .exe file is:

    AppDomain.CurrentDomain.BaseDirectory
    

    To get the current directory:

    Environment.CurrentDirectory
    
    0 讨论(0)
  • 2020-12-07 13:35

    Application.StartUpPath;

    0 讨论(0)
  • 2020-12-07 13:42

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

    0 讨论(0)
  • 2020-12-07 13:44

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

    0 讨论(0)
  • 2020-12-07 13:48

    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\

    0 讨论(0)
  • 2020-12-07 13:53

    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)

    0 讨论(0)
提交回复
热议问题