Farrukh Niaz
Farrukh Niaz

Reputation: 61

How to detect whether “.Net Framework 4.6.1 Hotfix” is installed on System or not using C# Code?

Sometimes people install .net framework 4.6.1 and my application doesn't properly work in their environment unless they also installed the .Net Framework 4.6.1 Hotfix. So I have to sort this out explicitly on every client's machine. And also write a programm that does this check to help the people in support.

Here is the sample code which detects on client machine whether 4.6.1 or higher version is installed on client's machine but it lacks checking 4.6.1 Hotfix. Note: 64-bit Operating System is Pre-Requisite.

    private static bool CheckIfVCInstalledOrNot()
    {
        bool vcInstalled = true;
        try
        {
            if (!Utilities.RedistributablePackage.IsInstalled(Utilities.RedistributablePackageVersion.VC2015to2019x64))
            {
                if (!Utilities.RedistributablePackage.IsInstalled(Utilities.RedistributablePackageVersion.VC2017x64))
                {
                    if (!Utilities.RedistributablePackage.IsInstalled(Utilities.RedistributablePackageVersion.VC2015x64))
                    {
                        if (!Utilities.RedistributablePackage.IsInstalled(Utilities.RedistributablePackageVersion.VC2013x64))
                        {
                            vcInstalled = false;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utilities.Logging.ErrorLog(ex);
        }
        return vcInstalled;
    }

public enum RedistributablePackageVersion
{
    VC2013x64,
    VC2015x64,
    VC2017x64,
    VC2015to2019x64
};

public static class RedistributablePackage
{
    /// <summary>
    /// Check if a Microsoft Redistributable Package is installed.
    /// </summary>
    /// <param name="redistributableVersion">The package version to detect.</param>
    /// <returns><c>true</c> if the package is installed, otherwise <c>false</c></returns>
    public static bool IsInstalled(RedistributablePackageVersion redistributableVersion)
    {
        try
        {
            switch (redistributableVersion)
            {
                case RedistributablePackageVersion.VC2015to2019x64:
                    var parametersVc2015to2019x64 = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\DevDiv\VC\Servicing\14.0\RuntimeMinimum", false);
                    if (parametersVc2015to2019x64 == null) return false;
                    var vc2015to2019x64Version = parametersVc2015to2019x64.GetValue("Version");
                    if (((string)vc2015to2019x64Version).StartsWith("14"))
                    {
                        return true;
                    }
                    break;
                case RedistributablePackageVersion.VC2017x64:
                    var paths2017x64 = new List<string>
                    {
                        @"Installer\Dependencies\,,amd64,14.0,bundle",
                        @"Installer\Dependencies\VC,redist.x64,amd64,14.16,bundle" //changed in 14.16.x
                    };
                    foreach (var path in paths2017x64)
                    {
                        var parametersVc2017x64 = Registry.ClassesRoot.OpenSubKey(path, false);
                        if (parametersVc2017x64 == null) continue;
                        var vc2017x64Version = parametersVc2017x64.GetValue("Version");
                        if (vc2017x64Version == null) return false;
                        if (((string)vc2017x64Version).StartsWith("14"))
                        {
                            return true;
                        }
                    }
                    break;
                case RedistributablePackageVersion.VC2015x64:
                    var parametersVc2015x64 = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\Installer\Dependencies\{d992c12e-cab2-426f-bde3-fb8c53950b0d}", false);
                    if (parametersVc2015x64 == null) return false;
                    var vc2015x64Version = parametersVc2015x64.GetValue("Version");
                    if (((string)vc2015x64Version).StartsWith("14"))
                    {
                        return true;
                    }
                    break;
                case RedistributablePackageVersion.VC2013x64:
                    var parametersVc2013x64 = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\Installer\Dependencies\{050d4fc8-5d48-4b8f-8972-47c82c46020f}", false);
                    if (parametersVc2013x64 == null) return false;
                    var vc2013x64Version = parametersVc2013x64.GetValue("Version");
                    if (((string)vc2013x64Version).StartsWith("12"))
                    {
                        return true;
                    }
                    break;
            }
            return false;
        }
        catch (Exception)
        {
            return false;
        }
    }
}

Upvotes: 0

Views: 187

Answers (1)

Archlight
Archlight

Reputation: 2079

You need to find the registry DWORD value of the hotfix and query the registry for that value.

Now if you nee the hotfix 3154529 you can see that the registry value is "394297".

So to find out you can see from this Table that the value for .net framework 4.6.1 is larger or equal to 393295 and 4.6.1 with hotfix should be 394297 or larger (newer hotfix or servicepack applied).

Upvotes: 2

Related Questions