Future .NET Version Discovery Check

In order not to defeat the dead horse , however, I am looking for a way to detect the installed .NET frameworks. It seems that the solutions provided (in the links) are good until a new version of the framework is released, and then all bets are disabled. The reason for this is that the discovery depends on the registry keys, and it seems that the v4 structure has violated the agreement, and now additional steps need to be taken to discover the v4.

Is there a way to discover the .NET platform, which will also work when .NET v5 arrives.

EDIT: Well, for future generations of frustrated .NET Version searchers, here is the code to make this happen:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using Microsoft.Win32;

private List<string> GetInstalledDotNetFrameworks()
{
    string key = string.Empty;
    string version = string.Empty;
    List<string> frameworks = new List<string>();

    var matches = Registry.LocalMachine
                .OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP")
                .GetSubKeyNames().Where(keyname => Regex.IsMatch(keyname, @"^v\d"));


    // special handling for v4.0 (deprecated) and v4 (has subkeys with info)
    foreach (var item in matches)
    {
        switch (item)
        {
            case "v4.0":  // deprecated - ignore
                break;

            case "v4":// get more info from subkeys

                key = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\" + item;
                string[] subkeys = Registry.LocalMachine
                    .OpenSubKey(key)
                    .GetSubKeyNames();


                foreach (var subkey in subkeys)
                {
                    key = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\" + item + @"\" + subkey;
                    version = Registry.LocalMachine
                            .OpenSubKey(key)
                            .GetValue("Version").ToString();

                    version = string.Format("{0} ({1})", version, subkey);
                    frameworks.Add(version);
                }


                break;
            case "v1.1.4322":   // special case, as the framework does not follow convention
                frameworks.Add(item);
                break;
            default:

                try
                {
                    // get the Version value
                    key = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\" + item;
                    version = Registry.LocalMachine
                            .OpenSubKey(key)
                            .GetValue("Version").ToString();

                    frameworks.Add(version);

                }
                catch
                {
                    // most likely new .NET Framework got introduced and broke the convention
                }

                break;

        }

    }

    // sort the list, just in case the registry was not sorted
    frameworks.Sort();

    return frameworks;
}
+3
4

, (. ):

Microsoft.Win32.Registry.LocalMachine
    .OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP")
    .GetSubKeyNames().Where(keyname=>Regex.IsMatch(keyname,@"^v\d"))

: v2.0.50727, v3.0, v3.5, v4, v4.0. (, , ). , SOFTWARE\Microsoft\.NETFramework v2.0.50727, v3.0 v4.0.30319 - ehhh, , !

, , :-). http://support.microsoft.com/kb/318785 , , , , Install - , v4.0.

: , - , , v4 Client Full profiles. , RegistryKey IDisposable, , Dispose - ( ).

var versionList = new List<string>();
using(var ndpKey=Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP")) {
    Action<RegistryKey, Action<RegistryKey,string>> processKids = (node, action) => {
        foreach(var childname in node.GetSubKeyNames())
            using(var child = node.OpenSubKey(childname))
                action(child,childname);
    };

    Action<RegistryKey, Func<RegistryKey, bool>> visitDescendants = null;
    visitDescendants = (regkey, isDone) => {
        if(!isDone(regkey))
            processKids(regkey, (subkey, subkeyname)=>visitDescendants(subkey,isDone));
    };

    processKids(ndpKey, (versionKey, versionKeyName) => {
        if(Regex.IsMatch(versionKeyName,@"^v\d")) {
            visitDescendants(versionKey, key => {
                bool isInstallationNode = Equals(key.GetValue("Install"), 1) && key.GetValue("Version") != null;
                if(isInstallationNode)
                    versionList.Add(
                        key.Name.Substring(ndpKey.Name.Length+1) 
                        + (key.GetValue("SP")!=null ? ", service pack "+ key.GetValue("SP"):"")
                        + " ("+key.GetValue("Version")  +") "
                    );
                return isInstallationNode;
            });
        }
    });
}

versionList :

v2.0.50727, service pack 2 (2.0.50727.4927)   
v3.0, service pack 2 (3.0.30729.4926)   
v3.5, service pack 1 (3.5.30729.4926)   
v4\Client (4.0.30319)   
v4\Full (4.0.30319)   
+4

, ?:) ? , v4, , v5 ? app.config, , , , . , , , . , app.config , , , . , . v5, . v1.1 v2 , , - .

+3

fejesjoco. , ?

Framework (C:\Windows\Microsoft.NET\Framework), , Framework . 4.0 5.0, 4.0.

, , .

+3

?

0

Source: https://habr.com/ru/post/1782679/


All Articles