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 -
1 private string[] GetAssemblyLocation(string[] assemblyNames)
2 {
3 string [] locations = new string[assemblyNames.Length];
4
5 for (int loop = 0; loop <= assemblyNames.Length - 1; loop++)
6 {
7 locations[loop] = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && a.ManifestModule.Name == assemblyNames[loop]).Select(a => a.Location).FirstOrDefault();
8 }
9 return locations;
10 }
And this is how you call it -
1 string[] assemblyLocations = GetAssemblyLocation(new string[] { "System.Web.dll", "System.dll", "EntityFramework.dll" });