7

Possible Duplicate:
How do I find out what directory my console app is running in with C#?

How to get current working directory of a console that runs a program so I could resolve relative paths passed as program args?

Lets say I've put my program here: c:\tools\program.exe But I'm invoking it from various places. Lets say I'm here: C:\Users\Me\Documents\ and I run this command program.exe --src=somefile.txt --dest=subdir\otherfile.txt

Environment.CurrentDirectory and System.Reflection.Assembly.GetExecutingAssembly().Location will return c:\tools but I would like to be able to resolve somefile.txt and subdir\oterfile.txt paths that are relative to C:\Users\Me\Documents\.

====== UPDATE ======

Thank you for your help. It seems that Environment.CurrentDirectory works as expected. It turned out that in my case the problem was caused by Xenocode's Postbuild tool (now called Spoon Virtual Application Studio) that I occasionally use to "pack" all program's files (including dlls, config, etc.) into one executable. It's very handy, but in this case the "virtualization" feature messed up my program's Environment variables. I've managed to solve that issue.

Community
  • 1
  • 1
SiliconMind
  • 2,185
  • 4
  • 25
  • 49

2 Answers2

15

Environment.CurrentDirectory gives you the Current Working Directory

Let me show a simple example:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Startup: " + Environment.CurrentDirectory);
        Environment.CurrentDirectory = @"D:\temp";
        Console.WriteLine("After:" + Environment.CurrentDirectory);
    }
}

Now I create two folders named D:\temp and D:\temp2
I put the executable in D:\temp
Open a command prompt and set the current working directory with cd D:\temp2
From that directory I run ..\temp\mytestapp.exe

the output is

Startup: D:\temp2
After: D:\temp

As a curiosity:

this was the documentation for Environment.CurrentDirectory in Net 1.1

Gets and sets the fully qualified path of the current directory; that is, the directory from which this process starts.

and this is the documentation in NET 4.0

Gets or sets the fully qualified path of the current working directory.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • I don't get it. With your example I get `Startup: D:\temp`. Note that I do not run the program by providing full path to the executable - I've set PATH variable to point to the directory where the executable sits. – SiliconMind Nov 27 '12 at 15:59
  • Tested again, this time I have set the PATH variable to d:\temp and moved away the command prompt to another directory. Running from this other directory (without giving a partial path) it prints the name of this other directory and then changes to d:\temp. – Steve Nov 27 '12 at 16:36
  • I'm sorry, you were right - see my edit for details. – SiliconMind Nov 29 '12 at 13:26
2

Use the Environment and Path classes. http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx

var fqn = Path.Combine(Environment.CurrentDirectory, "somefile.txt")

But to play with your documents you need:

var fqn = Path.Combine(
     Environment.GetFolderPath(Environment.SpecialFolder.Person),
     "somefile.txt");

`fqn' is TLA for fully qualified name

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • `Environment.CurrentDirectory` returns the path where the executable is located, in my case `c:\tools\`. "My documents" was just an example - I can't hardcode that. – SiliconMind Nov 27 '12 at 15:24
  • 1
    I don't think so, Environment.CurrentDirectory gives the Current working Directory – Steve Nov 27 '12 at 15:26
  • @Steve - I've tested that. In my case `Environment.CurrentDirectory` returns `c:\tools`. That's where the executable is located. But I don't care about that path. I want to know the command line current directory - the directory where user was when he/she run the program. – SiliconMind Nov 27 '12 at 15:30
  • OP has two questions posed as one! First how to get a file in 'current directory'. Secondly how to get a file in the `My Documents` folder. – Richard Schneider Nov 27 '12 at 15:31
  • @RichardSchneider - `My Documents` is just an example. This can be any directory. – SiliconMind Nov 27 '12 at 15:35
  • 2
    `CurrentDirectory` is always where the user was when the program was invoked. It is NOT the same as ystem.Reflection.Assembly.GetExecutingAssembly().Location, unless some other got in your way! – Richard Schneider Nov 27 '12 at 15:44