记录所有使用的DLL的版本

Bob*_*Bob 6 .net logging compact-framework

我想记录我的.NET应用程序使用的所有DLL的版本.如果在启动时或首次使用每个DLL时生成日志输出并不重要.

我想到的第一个解决方案是迭代所有DLL文件,这些文件与我的程序集位于同一目录中.但这是我最好的选择吗?有没有更好的方法来做到这一点?重要的是,该解决方案也应该适用于.NET-Compact-Framework.

Zac*_*son 6

编辑: 我测试并验证了这适用于.NET Compact Framework.

您可以使用Mono.Cecil执行此操作,这是一个功能强大的工具,允许您在IL级别打开和检查.NET程序集,甚至无需将它们加载到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;
    }
}
Run Code Online (Sandbox Code Playgroud)