How do I get the paths of all the assemblies referenced by the currently executing assembly? GetReferencedAssmblies()
gives me the AssemblyName[]
s.
Following Hans Passant's answer, and since the CodeBase
property always contained null
, I came up with this. It might not find all assemblies since they might not all be already loaded. In my situation, I had to find all reference of a previously loaded assembly, so it worked well:
IEnumerable GetAssemblyFiles(Assembly assembly)
{
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
return assembly.GetReferencedAssemblies()
.Select(name => loadedAssemblies.SingleOrDefault(a => a.FullName == name.FullName)?.Location)
.Where(l => l != null);
}
Usage:
var assemblyFiles = GetAssemblyFiles(typeof(MyClass).Assembly);