0
static void Main(string[] args)
{
    Console.WriteLine(Environment.CurrentDirectory);

    Console.ReadLine();
}

The code is very simple, but accidentally it raised a question. If I press the "Run" button, I will get the output like:

C:\Users\Admin\source\repos\FeaturesTest\ConsoleApp1\bin\Debug

But if I invoke this program with cmd.exe or Powershell, the output will be the current directory of the console:

Example 1:
PS C:\Users\Admin> C:\Users\Admin\source\repos\FeaturesTest\ConsoleApp1\bin\Debug\ConsoleApp1.exe
C:\Users\Admin

PS C:\Users\Admin> _

Example 2:
PS C:\Users\Admin\Desktop> C:\Users\Admin\source\repos\FeaturesTest\ConsoleApp1\bin\Debug\ConsoleApp1.exe
C:\Users\Admin\Desktop

PS C:\Users\Admin\Desktop> _

If I invoke this program with another, the output will be the working directory of the latter (which invoke the former):

The code of ConsoleApp2 (the caller):

        static void Main(string[] args)
        {
            Process.Start(@"C:\Users\Admin\source\repos\FeaturesTest\ConsoleApp1\bin\Debug\ConsoleApp1.exe");
        }
The output of ConsoleApp1 (the called program, because ConsoleApp2 don't have output):

        C:\Users\Admin\source\repos\FeaturesTest\ConsoleApp2\bin\Debug

So, I repeat the question: How do Environment.CurrentDirectory work? I just want to know it.
Thank you.

EggEgg
  • 21
  • 3
  • 3
    Did you check the documentation? https://learn.microsoft.com/en-us/dotnet/api/system.environment.currentdirectory?view=net-5.0 It's very clear as to what value it returns: "Gets or sets the fully qualified path of the current working directory." – Camilo Terevinto Sep 04 '21 at 10:52
  • Please read the Microsoft documentation for the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw), especially the section about the function parameter `lpCurrentDirectory`. If the process starting another process does not specify explicitly a current working directory for the process to start, `CreateProcess` uses the current working directory of the current process for the new process. __Current directory__ is not explicitly equal __executable/script file directory__. – Mofi Sep 04 '21 at 10:58
  • Right click on a shortcut file (`.lnk` file) like one of the shortcuts on your desktop and click on last context menu item __Properties__. There is on tab __General__ the property __Start in__. That is the string passed by `explorer.exe` to `CreateProcess` for parameter `lpCurrentDirectory` on double clicking the shortcut file to start the executable. Run in a command prompt window `start /?` and read the output help. There can be used the option `/D path` which results in using `CreateProcess` by `cmd.exe` with `path` being the string for function parameter `lpCurrentDirectory`. – Mofi Sep 04 '21 at 11:14
  • Run in a PowerShell console the command `get-help start-process` and look on output help. There can be used the option `-WorkingDirectory` with this cmdlet to specify the current working directory for the started process, i.e. the string to use by `powershell.exe` on calling `CreateProcess` for the function parameter `lpCurrentDirectory`. A program (executable/script) expecting that the current directory is the directory of the executable/script file is a not good coded program because of that can be true by chance, but it is more likely that the current directory is any other directory. – Mofi Sep 04 '21 at 11:16
  • All programming and scripting languages have a possibility to get from within the program the full path of the executable/script file (and the full path of the script interpreter). A program referencing files/directories in same directory as executable/script file should never reference those files/directories with a path relative to current directory. A program should determine first the executable/script file path and concatenate that path with the file/directory names to get the full qualified file/directory name for all these files/directories. – Mofi Sep 04 '21 at 11:16
  • I recommend to read [Which is better for getting assembly location: GetAssembly().Location or GetExecutingAssembly().Location?](https://stackoverflow.com/a/27060089/3074564) and [How can I get the application's path in a .NET console application?](https://stackoverflow.com/questions/837488/) and [Best way to get application folder path](https://stackoverflow.com/questions/6041332). But please ignore all answers which makes implicit or explicit use of the current working directory path for referencing files/directories in the directory of the C# coded program. – Mofi Sep 04 '21 at 11:20

1 Answers1

0

By definition, if this process starts in the root directory of a local or network drive, the value of this property is the drive name followed by a trailing slash (for example, "C:"). If this process starts in a subdirectory, the value of this property is the drive and subdirectory path, without a trailing slash (for example, "C:\mySubDirectory").

https://learn.microsoft.com/en-us/dotnet/api/system.environment.currentdirectory?view=net-5.0

Enviroment.CurrentDirectory will give you the directory where the application is invoked. If you invoke the executable from a PowerShell, in C:\ the app will be wrapped inside this shell, there's no need to instantiate a new shell... If you double click the .exe file will prompt a new shell to run the application, so the directory it's where the .exe is.

Check also this question to check other methods to get a directory of a running application.

Best way to get application folder path

Douglas Ferreira
  • 434
  • 1
  • 7
  • 23