Question

I'm running my console app from a plain old command prompt. I'm running it two ways:

  1. using a relative path to the executable from what I think is the working directory. i.e.

    C:\Working>.\path\to\my.exe -fileToRead file.txt

  2. using a folder in my $PATH$. I.e.

    C:\Working>my.exe -fileToRead file.txt

file.txt is in C:\Working and my.exe is C:\Working\path\to. my.exe will output an XML log file to the working directory. In my mind that, that should be C:\Working, but the file actually ends up in C:\Working\path\to. This doesn't jive with all other command line applications.

I'm not doing anything weird or non-standard (that I know about). I've tried just using the file name for the XML file, "TestResult.xml" and also Path.Combine(Environment.CurrentDirectory, "TestResult.xml"). Both end up in the executable directory, not the directory from which I'm running. The command-line parameter file argument is being read properly, so I know that's working.

Clarification: Basically, my problem is that Environment.CurrentDirectory and Assembly.GetExecutingAssembly().Location are the same directory, but shouldn't be.

What am I doing wrong here? And how do I get the directory from which I execute, not the path to the executable? (I realize I have the exact opposite problem of many questions on stackoverflow)

Was it helpful?

Solution

The results you have using Environment.CurrentDirectory are not the one I get with a very simple program like this

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Environment=" + Environment.CurrentDirectory);
            Console.WriteLine("Assembly=" + Assembly.GetExecutingAssembly().Location);
        }
    }
}

Executing this little app from the command line gives always for the first line the directory where the command prompt is running and the second line always the directory where the assembly is located.
So, I suppose that your problem is caused by something different. Probably a change in the current directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top