Sunday, February 15, 2009

How to Get current Working Directory

In my day to day programming i came across problem how to get the current working directory in web, desktop or even if you call the dll(dynamic link library) . Let us start with the current working directory of the desktop applications.

Current Location in Desktop Application
When developing desktop application you often need the current working directory of your application, when you want to write your log file for your application in case if there is some error in you application and you want to track that errors. Here is the code how you can get the current working directory of your applications. Here is the code you can use to get the current working directory of your application.
string strEnviromentDirectory = Environment.CurrentDirectory;
string strIODirectory = Directory.GetCurrentDirectory();
string strApplicationStartPath = Application.StartupPath;
Here is the output of the above statements, you can see that the out put of the above statements are same, mean they return same value.

Current Location in Web Application
To get the current working directory in the web application is very simple one like the desktop application. Here is the code you can use to get the current working directory of your web applications.
string strServerMapPath = Server.MapPath(".");
string srtRequestMapPath= Request.MapPath(".");
string strHttpRuntimePath = HttpRuntime.AppDomainAppPath;
The above statement return same result. The Server.MapPath() actually call the Request.MapPath internally to get the current working directory. And the third statement will return same result with the forward slashs("\\") appended at the end of the path.

You can see from the image that above three statement return same values execpt that the HttpRuntime.AppDomainAppPath will append the forward slash at the end of the return string.

Current Location in DLL(dynamic Link Library)
When using Dll in your application, here is the code you can use to get the dll working directory.

string strAssemblePath = Assembly.GetExecutingAssembly().CodeBase;
string strAssemblyLocation = Assembly.GetExecutingAssembly().Location;
The Assembly.GetExecutingAssembly() will return the assembly, that contains the code that is currently executing. When you are using desktop application then both the above statement return same result, except the CodeBase will append "file:///" before the full path of the dll.
But when you are using the web application then they both return different result. Because of the following reasons.
  1. ASP.NET application will always shadow copy all the private assemblies(in bin dir) to the temporary ASPNET folder......
  2. And the Assembly.Location point to the assembly's location after it has been shadow copied.....
Using the Assembly.CodeBase instead of the Assembly.Location , CodeBase point to the original path before shadow copied. here is the MSDN document on this:

The location of the loaded file that contains the manifest. If the loaded file was shadow-copied, the location is that of the file after being shadow-copied. If the assembly is loaded from a byte array, such as when using the Load(Byte[]) method overload, the value returned is an empty
string ("").

To get the location before the file has been shadow-copied, use the CodeBase property (Source)

All and any comments / bugs / suggestions are welcomed!

1 comment:

Anonymous said...

why they always have multiple ways to do one particular thing?