2

Following code iterates over all referenced assemblies of MyAssembly.dll. How can I output the physically filename of the referenced assemblies? So how can I get the filename of an AssemblyName-Object?

Assembly myAssembly = Assembly.LoadFile(@"C:\MyAssembly.dll");
foreach (AssemblyName lRefAsm in myAssembly.GetReferencedAssemblies())
{
   ...
}

E.g. the ToString()-method of AssemblyName only outputs things like this:

System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

An I don't find any property which gives me the filename.

rkhb
  • 14,159
  • 7
  • 32
  • 60
user1027167
  • 4,320
  • 6
  • 33
  • 40
  • Please try the Assembly.Location property – Oguz Ozgul Nov 20 '15 at 09:26
  • You want to get the chicken before the egg is laid. That's a pretty bad idea, assemblies are only loaded when they are necessary. By the jitter when it just-in-time compiles a method. Don't do this. Also never ever use Assembly.LoadFile(), always LoadFrom(). – Hans Passant Nov 20 '15 at 09:51

2 Answers2

0

You probably want to get the value of the Location property.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
0
        Assembly myAssembly = Assembly.GetExecutingAssembly();
        foreach (AssemblyName lRefAsm in myAssembly.GetReferencedAssemblies())
        {
            Assembly ass = Assembly.Load(lRefAsm.FullName);
            string codeBase = ass.CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            Console.WriteLine(Path.GetDirectoryName(path));
        }

I copied some code here.

Community
  • 1
  • 1
Lei Yang
  • 3,970
  • 6
  • 38
  • 59
  • This does not work in all cases. If lRefAsm.FullName is "System.Messaging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" the path will be a reference to Version 4.0.0.0 -> "C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Messaging/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Messaging.dll" – user1027167 Nov 23 '15 at 13:56
  • @user1027167 This is the GAC location, why wrong? Then what does the right path look like? – Lei Yang Nov 24 '15 at 01:28
  • In the example above I am requesting the version 2.0.0.0, but the return value is the newest version on my system which is 4.0.0.0. I think Assembly.Load() always returns the newest version on the system. – user1027167 Nov 24 '15 at 06:45