Skip to content

Get the current directory from a c# application

Sometimes i have to read/write files from the root directory of my application. Usually i did this with Environment.CurrentDirectory. Unfortunately, this only works if the application is a standalone application(.exe). In case the application is a windows service or a asp.net website, the path from Environment.CurrentDirectory is wrong.

There are several ways to get the root directory of an application, but so far i found only one which works in all scenarios. The only one which always works is:

//works...
IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath)

Here is a (incomplete) list of ways to get the root path of a c# application:

1. Windows service

Environment.CurrentDirectory:
//C:\Windows\system32
System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath)

//D:\project\WindowsServiceTest\WindowsServiceTest\bin\Debug AppDomain.CurrentDomain.BaseDirectory: //D:\project\WindowsServiceTest\WindowsServiceTest\bin\Debug\ Process.GetCurrentProcess().MainModule.FileName.Substring(0, Process.GetCurrentProcess().MainModule.FileName.LastIndexOf("\")): //D:\project\WindowsServiceTest\WindowsServiceTest\bin\Debug

2. Console application
Environment.CurrentDirectory:
//D:\project\ConsoleApplication3\ConsoleApplication3\bin\Debug
System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath):
//D:\project\ConsoleApplication3\ConsoleApplication3\bin\Debug
AppDomain.CurrentDomain.BaseDirectory:
//D:\project\ConsoleApplication3\ConsoleApplication3\bin\Debug\
Process.GetCurrentProcess().MainModule.FileName.Substring(0, Process.GetCurrentProcess().MainModule.FileName.LastIndexOf("\")):
//D:\project\ConsoleApplication3\ConsoleApplication3\bin\Debug
3. asp.net mvc3 website vs2010 integrated webserver (Debug mode)
Environment.CurrentDirectory:
//C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0
System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath):
//D:\project\MvcApplication1\MvcApplication1\bin
AppDomain.CurrentDomain.BaseDirectory:
//D:\project\MvcApplication1\MvcApplication1\
Process.GetCurrentProcess().MainModule.FileName.Substring(0, Process.GetCurrentProcess().MainModule.FileName.LastIndexOf("\")):
//C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0
4. asp.net mvc3 website IIS (Release mode)
Environment.CurrentDirectory: 
//c:\windows\system32\inetsrv 
System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath): 
//d:\project\MvcApplication1\MvcApplication1\bin 
AppDomain.CurrentDomain.BaseDirectory: 
//d:\project\MvcApplication1\MvcApplication1\ 
Process.GetCurrentProcess().MainModule.FileName.Substring(0, Process.GetCurrentProcess().MainModule.FileName.LastIndexOf("\")): 
//c:\windows\system32\inetsrv 

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

pregunton on :

Great.

What's about this applications:

Addin Visual Studio

VSIX Package Visual Studio

Windows Forms

WPF

Outlook Addin

custom assignment help on :

The Objective-C language is a simple computer language designed to enable sophisticated object-oriented programming. Objective-C is defined as a small but powerful set of extensions to the standard ANSI C language. Its additions to C are mostly based on Smalltalk, one of the first object-oriented programming languages. Objective-C is designed to give C full object-oriented programming capabilities, and to do so in a simple and straightforward way.

myxer on :

What else may I get that kind of info written in such a perfect approach?

expensive brands on :

This blog gives the way in which we can see the truth life. This is extremely pleasant one indepth data. A debt of gratitude is in order for this pleasant article. Fabulous thinking..!

vodafone sure signal on :

I always interesting to visit your blog. This is a fabulous thing to delivered our self. It is always an interesting topic for me and difficult to find out information about it.

MHRWriter on :

If your looking for Online Illinois license plate sticker renewals then you have need to come to the right place.We offer the fastest Illinois license plate sticker renewals in the state.

charlie on :

helpful site krowd login

krowd darden login on :

We are providing great hotel management services from our app to make your hotel business better then before.

essay edge coupon on :

That is good you have decided to win the competition through your innovative applications at any way. This is the Sporting spirit which every developer should have. I have written many such inspiring articles to people on my blogs on what a person needs to learn from them to become successful in life.

Best Tech Lipz on :

We are is serving in hotel management field to make their hotel business better then before . our first priority is to serve best to our honorable clients.

Social Magz on :

Great inspiring article. I am really enjoy showing your post because its very unique information for me. Thanks a lot for sharing this magnificent post.

Add Comment

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Markdown format allowed
Form options