Different ways of getting Path

It is a very common requirement to get the path of a file relative to the path of the current location which accesses the path. In .NET there are a number of ways which lets you determine the path of a file and there exists more than one valid ways to determine the current path of the file. Let us clear out doubts and use whichever best suits you.

1. Assembly.GetExecutingAssembly().CodeBase

The path returned by the above code is the actual path which was specified originally. If the assembly is downloaded from a server, it could virtually hold a http:// location, and cannot ensure it is to be a valid physical path always. If the assembly is installed into GAC, the path does not ensure its set.

To use the path you can use :

Assembly asmly = Assembly.GetAssembly(myobj.GetType()); // Assembly.GetExecutingAssembly();
Console.WriteLine(asmly.CodeBase);

2. Assembly.GetExecutingAssembly().Location

The path returned by the above code is the UNC path of the loaded file. This path generally conforms to the actual UNC path of the assembly file loaded to execute the code. Even though the assembly is loaded from GAC, the Location always gives the actual path of the Assembly from where it is loaded.

To use the path you can use :

Assembly asmly = Assembly.GetAssembly(myobj.GetType()); // Assembly.GetExecutingAssembly();
Console.WriteLine(asmly.Location);

//To get the Directory path
string theDirectory = System.IO.Path.GetDirectoryName(asmly.Location);

3. AppDomain.CurrentDomain.BaseDirectory

This path gives you the path of the Entrypoint of the application or from where the Current AppDomain is created. For instance, say your executable is executed on assembly a.exe, but the code is written in b.dll, the AppDomain.CurrentDomain.BaseDirectory always gives you the path of a.exe.

This is recommended for Windows Applications where you need to get Installation folder path, but not recommended for Web Applications.

string theDirectory = AppDomain.CurrentDomain.BaseDirectory

4. Environment.CurrentDirectory

The path returned is the current directory path of the Project. Generally if Environment path is set before executing the assembly, and thus it generally gives the Base directory path of the executing assembly, but working directory can change often even during application lifetime. Its best suited when you know the Environment.CurrentDirectory is set properly. You can use Environment.SetCurrentDirectory to set different working directory for the application.

var directory = Environment.CurrentDirectory

5. HttpRuntime.AppDomainAppPath

The path returned from the above code is the application directory path for the hosted application. For hosted application in IIS or from ASP.NET, this is the recommended path that you should use.

string webCurrentDirectory = HttpRunTime.AppDomainAppPath;

6. Server.MapPath

Server.MapPath returns the Physical path of the location from the Current directory for an http request. You can use ~ sign to determine whether it is relative to the current application path.

string path = Server.MapPath("~/myappfolder");

These are the different approaches to get Path of a directory. You are reluctant to use any of them based on your own requirements. It is best to consider all of them before selecting the best suited for you.

I hope this post will help you.

Thank you for reading.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434

2 Comments to “Different ways of getting Path”

Comments are closed.