zoukankan      html  css  js  c++  java
  • C# detect latest .net framework installed on PC

    
    
     static void GetNetVersionDemo()
            {
                using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@"SOFTWAREMicrosoftNET Framework SetupNDP"))
                {
                    foreach(var versionKeyName in ndpKey.GetSubKeyNames())
                    {
                        //Skip .NET Framework 4.5 version information.
                        if(versionKeyName=="v4")
                        {
                            continue;
                        }
    
                        if(versionKeyName.StartsWith("v"))
                        {
                            RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
    
                            //Get the .NET Framework version value.
                            var name = (string)versionKey.GetValue("Version", "");
    
                            //Get the service pack number.
                            var sp = versionKey.GetValue("SP", "").ToString();
    
                            //Get the installation flag,or an empty string if there is none.
                            var install = versionKey.GetValue("Install", "").ToString();
    
                            if(string.IsNullOrEmpty(install))
                            {
                                Console.WriteLine($"{versionKeyName} {name}");
                            }
                            else
                            {
                                if(!string.IsNullOrEmpty(sp) && install=="1")
                                {
                                    Console.WriteLine($"{versionKeyName} {name} SP{sp}");
                                }
                            }
    
                            if(!string.IsNullOrEmpty(name))
                            {
                                continue;
                            }
    
                            foreach(var subKeyName in versionKey.GetSubKeyNames())
                            {
                                RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                                name = (string)subKey.GetValue("Version", "");
                                if(!string.IsNullOrEmpty(name))
                                {
                                    sp = subKey.GetValue("SP", "").ToString();
                                }
    
                                install = subKey.GetValue("Install", "").ToString();
                                if(string.IsNullOrEmpty(install))
                                {
                                    Console.WriteLine($"{versionKeyName} {name}");
                                }
                                else
                                {
                                    if((!string.IsNullOrEmpty(sp)) && install=="1")
                                    {
                                        Console.WriteLine($"{subKeyName} {name}  SP{sp}");
                                    }
                                    else
                                        if(install=="1")
                                    {
                                        Console.WriteLine($"{subKeyName} {name}");
                                    }
                                }
                            }
                        }
                    }
                }
            }
    
    
    
    using Microsoft.Win32;
    
    static void Main(string[] args)
            {
                GetDotNetFrameworkVersion();
                Console.ReadLine();
            }
    
            static void GetDotNetFrameworkVersion()
            {
                const string subKey = @"SOFTWAREMicrosoftNET Framework SetupNDPv4Full";
                using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subKey))
                {
                    if(ndpKey!=null && ndpKey.GetValue("Release")!=null)
                    {
                        var objResult = ndpKey.GetValue("Release");
                        var versionResult = CheckFor45PlusVersion((int)objResult);
                        Console.WriteLine($".NET Framework Version:{versionResult}");
                    }
                    else
                    {
                        Console.WriteLine(".NET Framework Version 4.5 or later is not detected!");
                    }
                }
            }
    
            //Convert the Main.Minor.Build.Revision
            static string CheckFor45PlusVersion(int releaseKey)
            {             
                if(releaseKey>=528040)
                {
                   return "4.8 or later";
                }
    
                if(releaseKey>=461808)
                {
                    return "4.7.2";
                }
    
                if(releaseKey>=461308)
                {
                    return "4.7.1";
                }
    
                if (releaseKey >= 460798)
                {
                    return "4.7";
                }
    
                if(releaseKey>=394802)
                {
                    return "4.6.2";
                }
    
                if(releaseKey>=394254)
                {
                    return "4.6.1";
                }
    
                if(releaseKey>=393295)
                {
                    return "4.6";
                }
    
                if(releaseKey>=393295)
                {
                    return "4.5.2";
                }
    
                if(releaseKey>=378675)
                {
                    return "4.5.1";
                }
    
                if(releaseKey>=378389)
                {
                    return "4.5";
                }
    
                return "No 4.5 or later version detected!";
            }
  • 相关阅读:
    vue自定义指令:v-drag使用 拖动拖拽
    element ui 中动态添加的树形结构(带删除功能的),不管点击删除哪个都会删除掉最后一个
    element ui 表格中的插槽用法
    报错:Uncaught SyntaxError: Unexpected token u in JSON at position 0
    vue报错:Duplicate keys detected: ' '. This may cause an update error
    el-input验证规则
    el-input 给v-model赋了默认值后不能编辑
    DC-2 渗透测试
    DC:1渗透测试
    CTF easytrick
  • 原文地址:https://www.cnblogs.com/Fred1987/p/11954327.html
Copyright © 2011-2022 走看看