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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With