Directory.GetCurrentDirectory () returns different results based on command line arguments

I hope someone can explain why Directory.GetCurrentDirectory () returns different results depending on how to pass command-line arguments to the application (done using args and dragging the folder through the application. EXE)

To jump straight into it, consider this piece of code:

public class Program { static void Main(string[] args) { Console.WriteLine("The current directory is {0}", Directory.GetCurrentDirectory()); if(args != null && args.Any()) Console.WriteLine("Command line arguments are {0}", String.Join(", ", args)); Console.ReadLine(); } } 


If you create and run it using the command line as shown below, the result will be what you expected. It will display the current directory in which the application is located.

 C:\Projects\ArgumentTest\ApplicationA\bin\Debug\ApplicationA.exe C:\mydirectory The current directory is C:\Projects\ArgumentTest\ApplicationA\bin\Debug\ Command line arguments are C:\mydirectory 


If you create and run this program by dragging files or folders through the application, you get different results. Instead of returning the expected result, instead of Directory.GetCurrentDirectory () it returns the path to the first file that you drag and drop through the application.


Currently, I have work for this problem, I really want to understand why this is happening.

Additional Information:

  • .NET 4.5
  • Windows 2012R2 (virtual machine)
  • Full administrator rights on the machine.

Hope someone can give some insight.

+6
source share
3 answers

I think the problem is your expectation. In particular, this bit:

It will display the current directory in which the application is located.

This is not what I expect from GetCurrentDirectory() . The current directory is a function of the calling context, not the application. If I run the executable file through the full or relative path (and not just foo.exe ), I expect GetCurrentDirectory() return the directory in which I am, and not the directory in which the application is located. In the case of dragging and dropping files, this: frankly, GetCurrentDirectory() is largely undefined, but the directory of the first file is not unreasonable.

+9
source

When you delete a file in an executable file, it launches it from the location of the file you dropped. For example, if you drop C: \ Path-To-File \ File.txt to C: \ Program \ ApplicationA.exe, then how you did the following from the command line:

 cd C:\Path-To-File C:\Program\ApplicationA.exe C:\Path-To-File\File.txt 

When you start the application manually (in the example above), you have control over the directory from which it works, so it matches what you expect.

+4
source

I am sure the problem is with the "Current Directory" property, which is different.

The current directory provides the current working directory, not where the executable is.

When you drag & drop, the current directory is set to the source (the drag source), and not to the target

+2
source

Source: https://habr.com/ru/post/969511/


All Articles