views:

55

answers:

2

Hi!

If I have a regular console application (or any other application for that matter) and drag a file onto the .exe file using windows explorer (in order to use the file as "command-line-input"), the current directory is set to some other directory (my home folder?), rather than the directory where the application is located. If I start the application by double-clicking it, the current directory is set correctly.

What is the reason for this and is there a way to use the application's folder as current directory?

Br, Chris

A: 

Searching for a reason I found this on autoitscript.com:

[The application] simply inherits whatever Explorer's working directory is

This is consistent with my own observations, for example with programmable keys on keyboards.

I work around is by resetting the working directory. In C#, you can do it like this:

System.Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

You would also have to do this when you start a mono application on Linux without using the command line.

A: 

That is exactly why your app should not be relying on working directories to begin with. The full path of the file will be specified on the command line. Parse the path from it if needed.

Remy Lebeau - TeamB
This is not always the case. If on Windows 7 I run `TestApp "test"` from the command line, argument[0] will be "TestApp" not the full path.
0xC0DEFACE
I was referring to the path that Windows passes for the clicked filename in the other parameter. That is always a full path when the path is being passed in as part of a file extension registration or shell extension.
Remy Lebeau - TeamB