Get dll file extension by System.Reflection.Assembly

1.4k views Asked by At

I am going to implement a function that can extract all referenced dll files in an exe.

I'm using "System.Reflection.Assembly" to do it.

But I have no idea how to get file extension.

For example, text.exe contains x.dll, y.dll and z.dll

Above code returns only x, y, z.

Anyone know how to to it?

Thank you all.

System.Reflection.Assembly assemblyFile = System.Reflection.Assembly.LoadFile(exePath);

System.Reflection.AssemblyName[] names = assemblyFile.GetReferencedAssemblies();

foreach (System.Reflection.AssemblyName data in names)
{
     listBox2.Items.Add(data.Name);
}
2

There are 2 answers

1
Jens Meinecke On
foreach (System.Reflection.AssemblyName data in names)
{
   listBox2.Items.Add(data.FullName);
}
2
Mark PM On

If you are looking for the full file name (eg x.dll) then use this:

        foreach (System.Reflection.AssemblyName data in names)
        {
            var assembly = System.Reflection.Assembly.ReflectionOnlyLoad(data.FullName);
            listBox2.Items.Add(System.IO.Path.GetFileName(assembly.Location));
        }