Finding the Assembly location

Finding the location of the running application is a task that is often hit. This is usually required to locate resources relative to the application, such as application settings or deployed files. There are many locations that can be used outside of the assemblies location that are often more appropriate, such as the Users’ Application Data directory, temporary directory or a database.

One example that I hit recently where those locations just didn’t fit the requirement was for an installation deployed configuration file, which is configured by the installer for application wide settings, and was not to be modified once deployed. This method had to be consistent as it would be used in a shared library by WinForms, Web applications and Office Add-ins. Environment.CurrentDirectory, is usually set to the directory the application was started in, so I skipped that and went straight to the method I’ve used before:


From a WinForms application it returned, as expected:

C:\Users\Matthew\Documents\Visual Studio 2008\Projects\FindAssemblyLocation\Application1\bin\Debug

However, when I was running it in a web application it returned a directory that was not very helpful:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\website1\ecf8f52a\3c164603\assembly\dl3\7948a1a3\8eb04ec1_9e9fc801

And within an Excel Add-in it returned:


My colleague mentioned another method, so I gave that one a whirl: (Note _Default is the name of a class in the assembly)


Unfortunately it returned the same as above in all instances.

After a little searching I found another:

Uri assemblyUri = new Uri(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase));

From a WinForm application it returned the same as above. From a Web application hosted by IIS it returned:

C:\Users\Matthew\Documents\Visual Studio 2008\Projects\WebSite1\PrecompiledWeb\WebSite1\bin

Note however, that this also returned a Local Temporary ASP.NET Files location when running under the Visual Studio ASP.NET Development Server, but this is to be expected. From an Excel Add-in it returned:

C:\Users\Matthew\Documents\Visual Studio 2008\Projects\FindAssemblyLocation\ExcelAddin1\bin\Debug

This method appears to work in WinForm applications, Console applications, Office Add-ins, and Web applications hosted in IIS.

Technorati Tags: ,,

2 thoughts on “Finding the Assembly location

  1. Excelente!!!! Exactamente lo que estaba buscando! Gracias!!

    Great!!!! Exactly what I’ve looking for! Thank you!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s