从输出

时间:2018-04-02 14:28:07

标签: c# .net

Here Windows显示了如何查找框架版本。

假设我只需要知道当前的版本,它是什么?我的意思是Windows提供的示例的输出给出:

v2.0.50727  2.0.50727.4016  SP2
v3.0  3.0.30729.4037  SP2
v3.5  3.5.30729.01  SP1
v4
  Client  4.0.30319
  Full  4.0.30319 

我不需要所有SP的东西,我只是框架版本,例如:

4.6.2

如何“翻译”输出?

1 个答案:

答案 0 :(得分:0)

我修改了您通过链接提供的代码。我通过列表获取版本并在main方法中打印它。请尝试此代码

    public static void Main()
    {
        List<string> version = GetVersionFromRegistry();

        Console.WriteLine(".Net Versions");

        foreach (var item in version)
        {
            Console.WriteLine(item);
        }

        Console.ReadLine();
    }

    private static List<string> GetVersionFromRegistry()
    {
        List<string> output = new List<string>();
        // Opens the registry key for the .NET Framework entry.
        using (RegistryKey ndpKey =
            RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
            OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
        {
            // 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(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            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.
                    {
                        if (sp != "" && install == "1")
                        {
                            output.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.
                            output.Add(name);
                        else
                        {
                            if (sp != "" && install == "1")
                            {
                                output.Add(name);
                            }
                            else if (install == "1")
                            {
                                output.Add(name);
                            }
                        }
                    }
                }
            }

            return output;
        }
    }
}