我正在尝试通过程序集检查其他 .NET 应用程序使用的框架版本。我找到了两种获取框架版本的方法(首先通过 ImageRunetimeVersion 和程序集的全名),但我从中得到了两个不同的值,我不知道哪个是正确的:
Assembly ass = Assembly.LoadFrom(autPath);
string imageRuntimeVersion = ass.ImageRuntimeVersion;
Console.WriteLine("ImageRunetimeVersion: " + imageRuntimeVersion);
Console.WriteLine("FullName: " + ass.FullName);
Console.WriteLine("");
Console.WriteLine("----");
Console.WriteLine("Referenced Assemblies: ");
Console.WriteLine("");
AssemblyName[] referencedAssemblies = ass.GetReferencedAssemblies();
foreach (AssemblyName a in referencedAssemblies)
{
Console.WriteLine(a.FullName);
}
如果我要用我的应用程序和例如 paint.net 测试它,结果是:
如您所见,我不能说哪个“版本”是正确的。最大的问题是,如果我要查看我的 .net 应用程序的项目属性,目标平台是 3.5 而不是 2.0 或 1.0-
最佳答案
我想我可以为您解决一些问题。首先,FullName 属性为您提供应用程序版本号。这是您设置的数字,与 .NET Framework 版本无关。这意味着可以忽略 FullName 属性中的版本号。
imageRuntimeVersion 是 CLR 版本。不幸的是,2.0 涵盖了 .NET 2.0、3.0 和 3.5。从技术上讲,您的应用程序为您提供了正确的信息,但它并不是您真正想要的信息(我不认为)。
这是一篇有更多解释的 SO 文章: Retrieve Target Framework Version and Target Framework Profile from a .Net Assembly
那篇文章为您提供了一些建议,包括寻找可以为您提供目标框架的配置文件或查看所用库的版本。两者都不是万无一失的,但据我所知,这是你能做的最好的事情。
关于c# 使用 Assembly 从应用程序获取框架/运行时版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10105464/