首页 > 解决方案 > 如何检查设备上是否安装了 Microsoft Visual C++ 2015 Redistributable

问题描述

正如标题所说,我目前找不到这个问题的任何答案。

我目前正在使用 C# 进行检查。

大多数答案适用于 2013 版及以下版本。

如果大家有什么建议,请分享。

谢谢。

标签: c#

解决方案


很难获得 VC 2015 的所有注册表值,所以我编写了一个小函数,它将遍历所有依赖项并匹配指定版本(C++ 2015 x86)

public static bool IsVC2015x86Installed()
{
    string dependenciesPath = @"SOFTWARE\Classes\Installer\Dependencies";

    using (RegistryKey dependencies = Registry.LocalMachine.OpenSubKey(dependenciesPath))
    {
        if (dependencies == null) return false;

        foreach (string subKeyName in dependencies.GetSubKeyNames().Where(n => !n.ToLower().Contains("dotnet") && !n.ToLower().Contains("microsoft")))
        {
            using (RegistryKey subDir = Registry.LocalMachine.OpenSubKey(dependenciesPath + "\\" + subKeyName))
            {
                var value = subDir.GetValue("DisplayName")?.ToString() ?? null;
                if (string.IsNullOrEmpty(value)) continue;

                if (Regex.IsMatch(value, @"C\+\+ 2015.*\(x86\)")) //here u can specify your version.
                {
                    return true;
                }
            }
        }
    }

    return false;
}

依赖项:

using System;
using System.Text.RegularExpressions;
using Microsoft.Win32;

编辑:

C++ 2017 是 C++ 2015 的有效替代品,因此如果您还想检查它,请像这样编辑正则表达式:

Regex.IsMatch(value, @"C\+\+ (2015|2017).*\(x86\)")

推荐阅读