Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm hoping someone can explain why Directory.GetCurrentDirectory() returns different results based on how I pass my command line arguments to the application (running with args vs dragging a folder over the app.exe)

To jump right 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 build and run this using the command prompt as shown below the output is what you'd expect. It will output the current directory the application resides in.

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 build and run this program by dragging files or folders over the application you get different results. Instead of returning the expected result instead Directory.GetCurrentDirectory() returns the path to the first file you've dragged over the application.


I've currently got a work around for this issue however, I am keen to understand why this is happening.

Other info:

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

Hopefully someone can provide some insight.

like image 556
Svenkle Avatar asked May 19 '14 09:05

Svenkle


People also ask

What does GetCurrentDirectory return?

The GetCurrentDir function returns the current directory. Note: When you are specifying a file in your script, always specify the full path (using the appropriate InstallShield system variable, for example, SRCDIR) rather than depend on the current folder having the appropriate value.

How do I find my current path Ubuntu?

To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd.


2 Answers

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

It will output the current directory the application resides in.

That is not what I expect from GetCurrentDirectory(). The current directory is a feature of the calling context, not the application. If I run the executable via a full or relative path (rather than just foo.exe), I expect GetCurrentDirectory() to return the directory that I am in - not the directory that the application is in. In the case of dragging files over it: frankly, GetCurrentDirectory() is largely undefined, but the directory of the first file is not unreasonable.

like image 126
Marc Gravell Avatar answered Sep 22 '22 12:09

Marc Gravell


When you drop a file on to the executable, it will run it from the location of the file you've dropped. For example if you drop C:\Path-To-File\File.txt on to C:\Program\ApplicationA.exe it's like you've done the following from a command prompt:

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

When you run the application manually (in your example above) you've got control over the directory it's running from which is why it matches what you'd expect.

like image 31
Geekrocket Avatar answered Sep 20 '22 12:09

Geekrocket