C# 检查应用程序是否安装在注册表中
现在我用它列出注册表中列出的所有32位&64位的应用程序。 我已经看到了其他一些示例,说明了如何检查应用程序是否在没有任何运气的情况下安装C# 检查应用程序是否安装在注册表中,c#,c,windows,registry,C#,C,Windows,Registry,现在我用它列出注册表中列出的所有32位&64位的应用程序。 我已经看到了其他一些示例,说明了如何检查应用程序是否在没有任何运气的情况下安装 string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey); if (key != null) { foreach (String
string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
foreach (String a in key.GetSubKeyNames())
{
RegistryKey subkey = key.OpenSubKey(a);
Console.WriteLine(subkey.GetValue("DisplayName"));
}
}
registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
foreach (String a in key.GetSubKeyNames())
{
RegistryKey subkey = key.OpenSubKey(a);
Console.WriteLine(subkey.GetValue("DisplayName"));
}
}
因此,这个代码段在控制台窗口中列出了所有内容,我尝试做的是
只需从显示名称列表中找到一个程序标题,查看是否已安装
我最后一次尝试是
if (subkey.Name.Contains("OpenSSL"))
Console.Writeline("OpenSSL Found");
else
Console.Writeline("OpenSSL Not Found");
我试过的任何东西要么是假的,要么是假阳性。有没有人能告诉我如何从列表中抓取一个标题
请不要发布著名的私有静态void IsApplicationInstalled(p_name)函数。它对我根本不起作用。在搜索和排除故障后,我让它以这种方式工作:
public static bool checkInstalled (string c_name)
{
string displayName;
string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
{
displayName = subkey.GetValue("DisplayName") as string;
if (displayName != null && displayName.Contains(c_name))
{
return true;
}
}
key.Close();
}
registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
{
displayName = subkey.GetValue("DisplayName") as string;
if (displayName != null && displayName.Contains(c_name))
{
return true;
}
}
key.Close();
}
return false;
}
我只是简单地称之为使用
if(checkInstalled("Application Name"))
这是一种干净的方法,不需要那么多代码
private static bool IsSoftwareInstalled(string softwareName)
{
var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall") ??
Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
if (key == null)
return false;
return key.GetSubKeyNames()
.Select(keyName => key.OpenSubKey(keyName))
.Select(subkey => subkey.GetValue("DisplayName") as string)
.Any(displayName => displayName != null && displayName.Contains(softwareName));
}
用if语句调用它:
if (IsSoftwareInstalled("OpenSSL"))
我已经检查了@Stellan Lindell的代码,但它并非在所有情况下都有效。 我的版本应适用于所有情况,并检查已安装程序的特定版本(x86、x64)
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Win32;
名称空间测试
{
内部课程计划
{
公共枚举程序版本
{
x86,
x64
}
私有静态IEnumerable GetRegisterSubkey(RegistryKey RegistryKey)
{
返回registryKey.GetSubKeyNames()
.选择(registryKey.OpenSubKey)
.Select(subkey=>subkey.GetValue(“DisplayName”)作为字符串);
}
私有静态bool CheckNode(RegistryKey RegistryKey,字符串applicationName,ProgramVersion?ProgramVersion)
{
返回GetRegisterSubkeys(registryKey).Any(displayName=>displayName!=null
&&displayName.Contains(应用程序名)
&&displayName.Contains(programVersion.ToString());
}
私有静态bool CheckApplication(字符串注册表项、字符串应用程序名称、ProgramVersion?ProgramVersion)
{
RegistryKey key=Registry.LocalMachine.OpenSubKey(RegistryKey);
if(key!=null)
{
if(检查节点(键、应用程序名、程序版本))
返回true;
key.Close();
}
返回false;
}
已安装公共静态bool IsSoftwareInstalled(字符串应用程序名、ProgramVersion?ProgramVersion)
{
字符串[]注册表项=新建[]{
@“软件\Microsoft\Windows\CurrentVersion\Uninstall”,
@“软件\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall”
};
返回registryKey.Any(key=>CheckApplication(key,applicationName,programVersion));
}
私有静态void Main()
{
//例子
WriteLine(“Notepad++:”+IsSoftwareInstalled(“Notepad++”,null));
WriteLine(“Notepad++(x86):”+IsSoftwareInstalled(“Notepad++”,ProgramVersion.x86));
WriteLine(“Notepad++(x64):”+IsSoftwareInstalled(“Notepad++”,ProgramVersion.x64));
控制台。Wrad Lead(微软Visual C++ 2009:+ ISSualAdAs安装(微软Visual C++ 2009),NULL);
WriteLine(“MicrosoftVisualC-2009:+IsSoftwareInstalled”(“MicrosoftVisualC-2009”,null));
控制台。Wrad Lead(微软Visual C++ 2013:+ ISSualAdAs安装(微软Visual C++ 2013),NULL);
控制台。WrangLink(“微软Visual C++ 2012可重新分发(x86)”:“+ ISSualAdvice”(微软Visual C++ 2013),程序版本.x86);
控制台。WrangLink(“微软Visual C++ 2012可重新分发(X64)”:“+ ISSualAdvice”(微软Visual C++ 2013),程序版本.x64);
Console.ReadKey();
}
}
}
上述解决方案确实不错,但有时您必须检查产品是否也安装在另一台机器上。因此,有一个版本基于上述@Stellan Lindell和@Mroczny Arturek的解决方案
此方法适用于本地计算机和远程计算机
public static bool IsSoftwareInstalled(string softwareName, string remoteMachine = null, StringComparison strComparison = StringComparison.Ordinal)
{
string uninstallRegKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryView[] enumValues = (RegistryView[])Enum.GetValues(typeof(RegistryView));
//Starts from 1, because first one is Default, so we dont need it...
for (int i = 1; i < enumValues.Length; i++)
{
//This one key is all what we need, because RegView will do the rest for us
using (RegistryKey key = (string.IsNullOrWhiteSpace(remoteMachine))
? RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, enumValues[i]).OpenSubKey(uninstallRegKey)
: RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, remoteMachine, enumValues[i]).OpenSubKey(uninstallRegKey))
{
if (key != null)
{
if (key.GetSubKeyNames()
.Select(keyName => key.OpenSubKey(keyName))
.Select(subKey => subKey.GetValue("DisplayName") as string)
//SomeTimes we really need the case sensitive/insensitive option...
.Any(displayName => displayName != null && displayName.IndexOf(softwareName, strComparison) >= 0))
{ return true; }
}
}
}
return false;
}
有我的SelectStringsFromWMI方法,但您可以自己完成,这不是此解决方案的重要部分。但是如果你感兴趣的话,那就是
public static List<Dictionary<string, string>> SelectStringsFromWMI(SelectQuery select, ManagementScope wmiScope)
{
List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiScope, select))
{
using (ManagementObjectCollection objectCollection = searcher.Get())
{
foreach (ManagementObject managementObject in objectCollection)
{
//With every new object we add new Dictionary
result.Add(new Dictionary<string, string>());
foreach (PropertyData property in managementObject.Properties)
{
//Always add data to newest Dictionary
result.Last().Add(property.Name, property.Value?.ToString());
}
}
return result;
}
}
}
public static List SelectStringsFromWMI(SelectQuery-select,ManagementScope-wmiScope)
{
列表结果=新列表();
使用(ManagementObjectSearcher search=新的ManagementObjectSearcher(wmiScope,选择))
{
使用(ManagementObjectCollection objectCollection=searcher.Get())
{
foreach(对象集合中的ManagementObject ManagementObject)
{
//对于每个新对象,我们都会添加新字典
添加(新字典());
foreach(managementObject.Properties中的PropertyData属性)
{
//始终将数据添加到最新字典
result.Last().Add(property.Name,property.Value?.ToString());
}
}
返回结果;
}
}
}
!!更新强>
由于性能非常差,还有另一个改进。只需异步获取值
public static bool IsSoftwareInstalled(string softwareName, string remoteMachine = null, StringComparison strComparison = StringComparison.Ordinal)
{
string uninstallRegKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryView[] enumValues = (RegistryView[])Enum.GetValues(typeof(RegistryView));
//Starts from 1, because first one is Default, so we dont need it...
for (int i = 1; i < enumValues.Length; i++)
{
//This one key is all what we need, because RegView will do the rest for us
using (RegistryKey regKey = (string.IsNullOrWhiteSpace(remoteMachine))
? RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, enumValues[i]).OpenSubKey(uninstallRegKey)
: RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, remoteMachine, enumValues[i]).OpenSubKey(uninstallRegKey))
{
if (SearchSubKeysForValue(regKey, "DisplayName", softwareName, strComparison).Result)
{ return true; }
}
}
return false;
}
publicstaticboolsoftwareinstalled(stringsoftwarename,stringremotemachine=null,StringComparison=StringComparison.Ordinal)
{
字符串uninstallRegKey=@“SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”;
RegistryView[]enumValues=(RegistryView[])Enum.GetValues(typeof(RegistryView));
//从1开始,因为第一个是默认值,所以我们不需要它。。。
for(int i=1;i public static List<Dictionary<string, string>> SelectStringsFromWMI(SelectQuery select, ManagementScope wmiScope)
{
List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiScope, select))
{
using (ManagementObjectCollection objectCollection = searcher.Get())
{
foreach (ManagementObject managementObject in objectCollection)
{
//With every new object we add new Dictionary
result.Add(new Dictionary<string, string>());
foreach (PropertyData property in managementObject.Properties)
{
//Always add data to newest Dictionary
result.Last().Add(property.Name, property.Value?.ToString());
}
}
return result;
}
}
}
public static bool IsSoftwareInstalled(string softwareName, string remoteMachine = null, StringComparison strComparison = StringComparison.Ordinal)
{
string uninstallRegKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryView[] enumValues = (RegistryView[])Enum.GetValues(typeof(RegistryView));
//Starts from 1, because first one is Default, so we dont need it...
for (int i = 1; i < enumValues.Length; i++)
{
//This one key is all what we need, because RegView will do the rest for us
using (RegistryKey regKey = (string.IsNullOrWhiteSpace(remoteMachine))
? RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, enumValues[i]).OpenSubKey(uninstallRegKey)
: RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, remoteMachine, enumValues[i]).OpenSubKey(uninstallRegKey))
{
if (SearchSubKeysForValue(regKey, "DisplayName", softwareName, strComparison).Result)
{ return true; }
}
}
return false;
}
public static async Task<bool> SearchSubKeysForValue(RegistryKey regKey, string valueName, string searchedValue, StringComparison strComparison = StringComparison.Ordinal)
{
bool result = false;
string[] subKeysNames = regKey.GetSubKeyNames();
List<Task<bool>> tasks = new List<Task<bool>>();
for (int i = 0; i < subKeysNames.Length - 1; i++)
{
//We have to save current value for i, because we cannot use it in async task due to changed values for it during foor loop
string subKeyName = subKeysNames[i];
tasks.Add(Task.Run(() =>
{
string value = regKey.OpenSubKey(subKeyName)?.GetValue(valueName)?.ToString() ?? null;
return (value != null && value.IndexOf(searchedValue, strComparison) >= 0);
}));
}
bool[] results = await Task.WhenAll(tasks).ConfigureAwait(false);
result = results.Contains(true);
return result;
}
Console.WriteLine(IsSoftwareInstalled("Notepad++"));
public static bool IsSoftwareInstalled(string softwareName)
{
var registryUninstallPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
var registryUninstallPathFor32BitOn64Bit = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
if (Is32BitWindows())
return IsSoftwareInstalled(softwareName, RegistryView.Registry32, registryUninstallPath);
var is64BitSoftwareInstalled = IsSoftwareInstalled(softwareName, RegistryView.Registry64, registryUninstallPath);
var is32BitSoftwareInstalled = IsSoftwareInstalled(softwareName, RegistryView.Registry64, registryUninstallPathFor32BitOn64Bit);
return is64BitSoftwareInstalled || is32BitSoftwareInstalled;
}
private static bool Is32BitWindows() => Environment.Is64BitOperatingSystem == false;
private static bool IsSoftwareInstalled(string softwareName, RegistryView registryView, string installedProgrammsPath)
{
var uninstallKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView)
.OpenSubKey(installedProgrammsPath);
if (uninstallKey == null)
return false;
return uninstallKey.GetSubKeyNames()
.Select(installedSoftwareString => uninstallKey.OpenSubKey(installedSoftwareString))
.Select(installedSoftwareKey => installedSoftwareKey.GetValue("DisplayName") as string)
.Any(installedSoftwareName => installedSoftwareName != null && installedSoftwareName.Contains(softwareName));
}
string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey key64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey key = key64.OpenSubKey(registryKey);
public static string[] checkInstalled(string findByName)
{
string[] info = new string[3];
string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
//64 bits computer
RegistryKey key64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey key = key64.OpenSubKey(registryKey);
if (key != null)
{
foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
{
string displayName = subkey.GetValue("DisplayName") as string;
if (displayName != null && displayName.Contains(findByName))
{
info[0] = displayName;
info[1] = subkey.GetValue("InstallLocation").ToString();
info[2] = subkey.GetValue("Version").ToString();
}
}
key.Close();
}
return info;
}
string[] JavaVersion = Software.checkInstalled("Java(TM) SE Development Kit");