首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >有没有简单的方法来检查.NET框架的版本?

有没有简单的方法来检查.NET框架的版本?
EN

Stack Overflow用户
提问于 2009-06-05 01:06:25
回答 16查看 120.7K关注 0票数 93

问题是我需要知道它是否是3.5SP1版本,Environment.Version()只返回2.0.50727.3053

我找到了this solution,但我认为它会花费更多的时间,所以我正在寻找一个更简单的。有可能吗?

EN

回答 16

Stack Overflow用户

发布于 2017-04-19 20:24:43

在不需要访问注册表的权限的情况下,另一种方法是检查特定框架更新中引入的类的存在。

代码语言:javascript
复制
private static bool Is46Installed()
{
    // API changes in 4.6: https://github.com/Microsoft/dotnet/blob/master/releases/net46/dotnet46-api-changes.md
    return Type.GetType("System.AppContext, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false) != null;
}

private static bool Is461Installed()
{
    // API changes in 4.6.1: https://github.com/Microsoft/dotnet/blob/master/releases/net461/dotnet461-api-changes.md
    return Type.GetType("System.Data.SqlClient.SqlColumnEncryptionCngProvider, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false) != null;
}

private static bool Is462Installed()
{
    // API changes in 4.6.2: https://github.com/Microsoft/dotnet/blob/master/releases/net462/dotnet462-api-changes.md
    return Type.GetType("System.Security.Cryptography.AesCng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false) != null;
}

private static bool Is47Installed()
{
    // API changes in 4.7: https://github.com/Microsoft/dotnet/blob/master/releases/net47/dotnet47-api-changes.md
    return Type.GetType("System.Web.Caching.CacheInsertOptions, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false) != null;
}
票数 16
EN

Stack Overflow用户

发布于 2009-06-04 18:41:36

Environment.Version()给出了一个不同问题的正确答案。在.NET 2.0、3和3.5中使用相同版本的CLR。我想您可以在GAC中查看这些后续版本中添加的库。

票数 11
EN

Stack Overflow用户

发布于 2019-06-04 14:53:55

我遇到过这样一种情况:可能会从.NET 4.0或更高版本的程序集中调用我的.NET 4.0类库。

特定的方法调用仅在从4.5+程序集中执行时才起作用。

为了决定是否应该调用该方法,我需要确定当前执行的框架版本,这是一个很好的解决方案

代码语言:javascript
复制
var frameworkName = new System.Runtime.Versioning.FrameworkName(
    AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName
);

if (frameworkName.Version >= new Version(4, 5)) 
{
    // run code
}

更新

这在4.0程序集中起作用

var featureEnabled = GetCurrentFrameworkName().Version >=新版本(4,5);

代码语言:javascript
复制
public static FrameworkName GetCurrentFrameworkName()
{
    // AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName
    // is missing prior to .NET 4.5
    var targetFrameworkName = (string)AppDomain
        .CurrentDomain
        .SetupInformation
        .GetType()
        .GetProperty("TargetFrameworkName")?
        .GetValue(AppDomain.CurrentDomain.SetupInformation, null) ?? ".NETFramework,Version=v4.0.0";

    return new FrameworkName(targetFrameworkName);
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/951856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档