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.