Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log versions of all used DLLs

I want to log the versions of all DLLs my .NET-application uses. It doesn't matter if the log-output is generated on startup or on first use of each DLL.

The first solution which came to my mind was to iterate over all DLL files which reside in the same directory as my assembly. But is this the best option I have? Is there any better way to do this? It's important that the solution should also work on .NET-Compact-Framework.

like image 606
Bob Avatar asked Apr 06 '10 10:04

Bob


1 Answers

Edit: I tested and verified that this works on the .NET Compact Framework.

You could do this using Mono.Cecil, a powerful tool that allows you to open and inspect .NET assemblies at the IL level without even loading them into an AppDomain.

string path = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;

// If using Mono.Cecil 0.6.9.0:
AssemblyDefinition myAssembly = AssemblyFactory.GetAssembly(path);

// If using Mono.Cecil 0.9.1.0:
AssemblyDefinition myAssembly = AssemblyDefinition.ReadAssembly(path);

// from there, you can inspect the assembly's references
foreach (ModuleDefinition module in myAssembly.Modules)
{
    foreach (AssemblyNameReference assemblyReference in module.AssemblyReferences)
    {
        // do something with the reference e.g get name, version, etc
        string fullName = assemblyReference.FullName;
        Version version = assemblyReference.Version;
    }
}
like image 154
Zach Johnson Avatar answered Nov 24 '22 15:11

Zach Johnson