functions/dotnetver.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using Microsoft.Win32;
using System.Collections.Generic;

public class DotNetVer {
    public static List<string> GetVersionFromRegistry() {
        var result = new List<string>(); 
        result.AddRange(GetVersion4FromRegistry());
        result.AddRange(Get45or451FromRegistry());
        
        return result;
    }
    
    private static List<string> GetVersion4FromRegistry()
    {
        var result = 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();
                    
                    var fullname = "";
                    if (install == "") {
                        //no install info, must be later.
                        fullname = versionKeyName + " " + name;
                    }
                    else
                    {
                        if (sp != "" && install == "1")
                        {
                            fullname = versionKeyName + " " + name + " SP" + sp;
                        }
                    }
                    if (name != "")
                    {
                        result.Add(fullname);
                        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.
                            result.Add(versionKeyName + " " + name);
                        else
                        {
                            if (sp != "" && install == "1")
                            {
                                result.Add(fullname + " " + subKeyName + " " + name + " SP" + sp);
                            }
                            else if (install == "1")
                            {
                                result.Add(fullname + " " + subKeyName + " " + name);
                            }
                        }
                    }
                }
            }
        }
        return result;

    }
    
    
    private static List<string> Get45or451FromRegistry()
    {
        var result = new List<string>(); 
        using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\")) {
            if (ndpKey != null) {
                if(ndpKey.GetValue("Version") != null) {
                    result.Add(ndpKey.GetValue("Version") + " Full"); 
                }
                else if(ndpKey.GetValue("Release") != null) {
                    result.Add(CheckFor45DotVersion((int) ndpKey.GetValue("Release")));
                }
            } 
        }
        
        using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Client\\")) {
            if (ndpKey != null) {
                if(ndpKey.GetValue("Version") != null) {
                    result.Add(ndpKey.GetValue("Version") + " Client"); 
                }
                else if(ndpKey.GetValue("Release") != null) {
                    result.Add(CheckFor45DotVersion((int) ndpKey.GetValue("Release")) + " Client");
                }
            } 
        }
        
        return result;
    }
    
        // Checking the version using >= will enable forward compatibility, 
    // however you should always compile your code on newer versions of
    // the framework to ensure your app works the same.
    private static string CheckFor45DotVersion(int releaseKey)
    {
    if (releaseKey >= 393295) {
        return "4.6 or later";
    }
    if ((releaseKey >= 379893)) {
            return "4.5.2 or later";
        }
        if ((releaseKey >= 378675)) {
            return "4.5.1 or later";
        }
        if ((releaseKey >= 378389)) {
            return "4.5 or later";
        }
        // This line should never execute. A non-null release key should mean
        // that 4.5 or later is installed.
        return releaseKey.ToString();
    }
}