views:

170

answers:

3

My application requires that I have access to the file system. Regardless if the code runs on a web server or a console app, how can I get the root directory the application is running in. Something like C:\TheApplication...

Thanks!

+8  A: 

Easiest is probably:

System.Reflection.Assembly.GetExecutingAssembly().Location

Hope this helps,

-Oisin

x0n
Ahhhhh...I have my idiot hat on today ;) Totally forgot about that!
DDiVita
Out of curiosity, I wonder what is returned when the executing assembly is in the Global Assembly Cache?
John K
@jdk, just a guess, but wouldn't this return the calling EXE, rather than the assembly placing the reflection call?
harpo
A: 

You can try this:

String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

You can read more about the two classes here:

Lasse V. Karlsen
A: 
Jerry
I'm not a C# programmer, and I haven't looked up the docs, but just from the name of that I would expect it to return the path to the executable file, rather than the current working directory. I suspect the OP wants the working directory.
rmeador