此信息存储在程序集中,因为它是运行 EXE 文件所必需的。我用这个函数阅读了目标框架:
public string GetTargetFramework()
{
System.Reflection.Assembly assembly;
Attribute[] attributes;
System.Runtime.Versioning.TargetFrameworkAttribute targetFrameworkAttribute;
assembly = Assembly.GetEntryAssembly();
attributes = AssemblyDescriptionAttribute.GetCustomAttributes(assembly);
foreach(Attribute item in attributes)
{
targetFrameworkAttribute = item as System.Runtime.Versioning.TargetFrameworkAttribute;
if(targetFrameworkAttribute != null)
return targetFrameworkAttribute.FrameworkDisplayName;
}
return null; // "TargetFrameworkAttribute not found"
}
我特意明确了命名空间。