[英]Cannot find Microsoft Visual C++ 2015 Redistributable Programmatically C#
[英]How to Check if Microsoft Visual C++ 2015 Redistributable Installed on a Device
正如标题所说,我目前找不到这个问题的任何答案。
我目前正在使用 C# 进行检查。
大多数答案适用于 2013 及以下版本。
如果大家有什么建议,请分享。
谢谢。
基本答案是:如果它在运行时存在,请不要打扰。 将其放入您的安装程序中。 让它作为正常的“需要提升权限”安装过程的一部分来执行。
如果它已经存在,安装程序将什么也不做。
如果它不在那里,它现在将在管理员权限下运行,然后在那里。
如果它已损坏,希望安装人员会修复安装。
如果这以某种方式不起作用,那么您微不足道的用户代码将无法在运行时修复它。 这是管理员的工作。
每个安装程序都会这样做,其中最重要的是 Visual Studio 和 SQL Server 安装程序。 我所知道的唯一轻微修改是 Steam,它在第一次执行程序之前以提升的权限运行这些安装程序。 但这只是从稍微不同的角度“确定它在那里”。
我只知道一种程序员不会这样做:那个程序员从未在新安装的 Windows 上测试过他的程序(虚拟机工作),因此不知道甚至存在这些要求(因为每个其他程序都安装了 VC Redist 和当前的 DX 版本)。
很难获得 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\)")
正如评论和答案中提到的,一种方法是让安装程序运行并查看是否安装了更新的版本。 安装程序将显示错误并退出。 如果安装程序使用/quiet
标志运行,则不会显示任何错误。 另一种方法是简单地检查注册表值:
HKEY_LOCAL_MACHINE\\SOFTWARE[\\Wow6432Node]\\Microsoft\\VisualStudio\\vs-version\\VC\\Runtimes\\{x86|x64|ARM} key
这里 vs-version 是 Visual Studio 的版本(14.0 for Visual Studio 2015 和 2017)关键是 ARM、x86 或 x64,具体取决于平台。
版本号存储在REG_SZ
字符串值Version 中。 如果正在安装的软件包低于已安装的版本,则无需安装。
更多信息: https : //docs.microsoft.com/en-us/cpp/ide/redistributing-visual-cpp-files?view=vs-2017
我修改了 @ssamko 的版本以检查 x64 和 x86 可再发行组件。 希望它会帮助某人:
public static bool IsVC2015Installed()
{
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 (Environment.Is64BitOperatingSystem)
{
if (Regex.IsMatch(value, @"C\+\+ 2015.*\((x64|x86)\)"))
{
return true;
}
}
else
{
if (Regex.IsMatch(value, @"C\+\+ 2015.*\(x86\)"))
{
return true;
}
}
}
}
}
return false;
}
依赖项:
using System;
using System.Text.RegularExpressions;
using Microsoft.Win32;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.