前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#判断.NetFramework库是否安装

C#判断.NetFramework库是否安装

作者头像
码客说
发布2023-09-26 08:19:25
2460
发布2023-09-26 08:19:25
举报
文章被收录于专栏:码客码客

正文

代码语言:javascript
复制
namespace env_monitor.Utils
{
    using Microsoft.Win32;
    using System.Diagnostics;

    /// <summary>
    /// 检测.Net环境
    /// </summary>
    public class EnvCheckUtil
    {
        /// <summary>
        /// 判断.Net Framework的Version是否符合需要 (.Net Framework 版本在2.0及以上)
        /// </summary>
        /// <param name="version">
        /// 需要的版本 version = 4.5
        /// </param>
        /// <returns>
        /// </returns>
        public static bool IsInstallDotNet(string version)
        {
            string oldname = "0";
            using (RegistryKey ndpKey = RegistryKey.OpenRemoteBaseKey(
                           RegistryHive.LocalMachine,
                           ""
                       )
                       .OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            {
                if (ndpKey != null)
                    foreach (string versionKeyName in ndpKey.GetSubKeyNames())
                    {
                        if (versionKeyName.StartsWith("v"))
                        {
                            RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                            if (versionKey != null)
                            {
                                string newname = (string)versionKey.GetValue(
                                    "Version",
                                    ""
                                );
                                if (string.CompareOrdinal(
                                        newname,
                                        oldname
                                    ) >
                                    0)
                                {
                                    oldname = newname;
                                }
                                if (newname != "")
                                {
                                    continue;
                                }
                                foreach (string subKeyName in versionKey.GetSubKeyNames())
                                {
                                    RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                                    if (subKey != null)
                                        newname = (string)subKey.GetValue(
                                            "Version",
                                            ""
                                        );
                                    if (string.CompareOrdinal(
                                            newname,
                                            oldname
                                        ) >
                                        0)
                                    {
                                        oldname = newname;
                                    }
                                }
                            }
                        }
                    }
            }
            return string.CompareOrdinal(
                       oldname,
                       version
                   ) >
                   0;
        }

        /// <summary>
        /// 打开系统软件卸载页面
        /// </summary>
        public static void OpenAppUninstallPage()
        {
            Process.Start("appwiz.cpl");
        }
    }
}

调用

代码语言:javascript
复制
var isInstallNet = EnvCheckUtil.IsInstallDotNet("4.5");
Console.WriteLine(@"是否安装.Net4.5:" + isInstallNet);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 正文
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档