Finding the Location of a Running Assembly in .Net

For many reasons you might want to know where a running assembly is located on your filesystem.

I had to do this once when I was compiling classes at runtime and had a dependency on EntityFramework. Another reason might be verifying which dll is actually being used in an application.

Here’s the simple method to perform the search –

    private string[] GetAssemblyLocation(string[] assemblyNames)
    {
        string [] locations = new string[assemblyNames.Length];
 
        for (int loop = 0; loop <= assemblyNames.Length - 1; loop++)       
		{
             locations[loop] = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && a.ManifestModule.Name == assemblyNames[loop]).Select(a => a.Location).FirstOrDefault();
        }
        return locations;
    }

And this is how you call it –

	string[] assemblyLocations = GetAssemblyLocation(new string[] { "System.Web.dll", "System.dll", "EntityFramework.dll" });

Leave a Reply

Your email address will not be published. Required fields are marked *