zoukankan      html  css  js  c++  java
  • 使用C#检验.NET FrameWork版本

    代码如下:

    public static bool checkFrameWork(string destVersion)
            {
                bool ver1 = GetVersionFromRegistry(destVersion);
                if (ver1)
                    return true;
                else
                {
                    return Get45PlusFromRegistry();
                }
            }
            //reference:https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
            public static bool Get45PlusFromRegistry()
            {
                const string subkey = @"SOFTWAREMicrosoftNET Framework SetupNDPv4Full";
    
                using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
                {
                    if (ndpKey != null && ndpKey.GetValue("Release") != null)
                    {
                        return CheckFor45PlusVersion((int)ndpKey.GetValue("Release"));
                    }
                    return false;
                }
            }
    
            // Checking the version using >= will enable forward compatibility.
            public static bool CheckFor45PlusVersion(int releaseKey)
            {
                if (releaseKey >= 378389)
                    return true;
                // This code should never execute. A non-null release key should mean
                // that 4.5 or later is installed.
                return false;
            }
            public static bool GetVersionFromRegistry(string destVersion)
            {
                ArrayList versions = new ArrayList();
                // Opens the registry key for the .NET Framework entry.
                using (RegistryKey ndpKey =
                    RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
                    OpenSubKey(@"SOFTWAREMicrosoftNET Framework SetupNDP"))
                {
                    // As an alternative, if you know the computers you will query are running .NET Framework 4.5 
                    // or later, you can use:
                    // using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, 
                    // RegistryView.Registry32).OpenSubKey(@"SOFTWAREMicrosoftNET Framework SetupNDP"))
                    foreach (string versionKeyName in ndpKey.GetSubKeyNames())
                    {
                        if (versionKeyName.StartsWith("v"))
                        {
                            RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                            string name = (string)versionKey.GetValue("Version", "");
                            string sp = versionKey.GetValue("SP", "").ToString();
                            string install = versionKey.GetValue("Install", "").ToString();
                            if (install == "") //no install info, must be later.
                            {
                                Console.WriteLine(name);
                                versions.Add(name);
                            }
                            //Console.WriteLine(versionKeyName + "  " + name);
                            else
                            {
                                if (sp != "" && install == "1")
                                {
                                    //Console.WriteLine(versionKeyName + "  " + name + "  SP" + sp);
                                    Console.WriteLine(name);
                                    versions.Add(name);
                                }
    
                            }
                            if (name != "")
                            {
                                continue;
                            }
                            foreach (string subKeyName in versionKey.GetSubKeyNames())
                            {
                                RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                                name = (string)subKey.GetValue("Version", "");
                                if (name != "")
                                    sp = subKey.GetValue("SP", "").ToString();
                                install = subKey.GetValue("Install", "").ToString();
                                if (install == "") //no install info, must be later.
                                {
                                    //Console.WriteLine(versionKeyName + "  " + name);
                                    //Console.WriteLine(name);
                                    versions.Add(name);
                                }
                                else
                                {
                                    if (sp != "" && install == "1")
                                    {
                                        //Console.WriteLine("  " + subKeyName + "  " + name + "  SP" + sp);
                                        //Console.Write(name);
                                        versions.Add(name);
                                    }
                                    else if (install == "1")
                                    {
                                        //Console.WriteLine("  " + subKeyName + "  " + name);
                                        //Console.WriteLine(name);
                                        versions.Add(name);
                                    }
                                }
                            }
                        }
                    }
                    object[] verArr = versions.ToArray();
                    foreach (object o in verArr)
                    {
                        Version ver;
                        string str = o.ToString();
                        if (str == "")
                            ver = new Version("1.0");
                        else
                            ver = new Version(str);
                        if (ver >= new Version(destVersion))
                            return true;
                    }
                }
                return false;
            }
    

    其中还有一点小瑕疵,有兴趣的自己看一看修改一下就好了。
    在使用的时候,我们只需要调用checkFrameWork函数就可以了。

  • 相关阅读:
    JAVA安卓和C# 3DES加密解密的兼容性问题(2013年8月修改版)
    eval绑定decimal数据后,如何去掉后面没有意义的0?
    Linq使用Group By经验总结
    mysql 分页存储过程 一次返回两个记录集(行的条数,以及行记录),DataReader的Read方法和NextResult方法
    把 HttpHandler.ashx 修改为 异步编程 异步操作
    td内容自动换行 ,td超过宽度显示点点点… , td 使用 overflow:hidden 无效,英文 数字 不换行 撑破div容器
    window.location.href = window.location.href 跳转无反应 a 超链接 onclick 点击跳转无反应
    C#怎么调用百度地图Web API
    .Net MVC 当前上下文中不存在名称“Style”
    无法使用备份文件 'D:20160512.bak',因为原先格式化该文件时所用扇区大小为 512,而目前所在设备的扇区大小为 4096
  • 原文地址:https://www.cnblogs.com/DM428/p/8676678.html
Copyright © 2011-2022 走看看