现在我用这个来列出32位和64位registry中列出的所有应用程序。我已经看到了如何检查应用程序是否没有任何运气的其他例子。
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")); } }
所以这段代码将它列在控制台窗口中,我试图做的只是从显示名称列表中find一个程序标题,以查看它是否已安装。
我试过的最后一件事是
if (subkey.Name.Contains("OpenSSL")) Console.Writeline("OpenSSL Found"); else Console.Writeline("OpenSSL Not Found");
我试过的任何东西都回来了,或者是假的或者是误报。 有没有人能告诉我如何从列表中抓取一个标题?
请不要张贴知名的私人静态无效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)。
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Win32; namespace Test { internal class Program { public enum ProgramVersion { x86, x64 } private static IEnumerable<string> GetRegisterSubkeys(RegistryKey registryKey) { return registryKey.GetSubKeyNames() .Select(registryKey.OpenSubKey) .Select(subkey => subkey.GetValue("DisplayName") as string); } private static bool CheckNode(RegistryKey registryKey, string applicationName, ProgramVersion? programVersion) { return GetRegisterSubkeys(registryKey).Any(displayName => displayName != null && displayName.Contains(applicationName) && displayName.Contains(programVersion.ToString())); } private static bool CheckApplication(string registryKey, string applicationName, ProgramVersion? programVersion) { RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey); if (key != null) { if (CheckNode(key, applicationName, programVersion)) return true; key.Close(); } return false; } public static bool IsSoftwareInstalled(string applicationName, ProgramVersion? programVersion) { string[] registryKey = new [] { @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" }; return registryKey.Any(key => CheckApplication(key, applicationName, programVersion)); } private static void Main() { // Examples Console.WriteLine("Notepad++: " + IsSoftwareInstalled("Notepad++", null)); Console.WriteLine("Notepad++(x86): " + IsSoftwareInstalled("Notepad++", ProgramVersion.x86)); Console.WriteLine("Notepad++(x64): " + IsSoftwareInstalled("Notepad++", ProgramVersion.x64)); Console.WriteLine("Microsoft Visual C++ 2009: " + IsSoftwareInstalled("Microsoft Visual C++ 2009", null)); Console.WriteLine("Microsoft Visual C-- 2009: " + IsSoftwareInstalled("Microsoft Visual C-- 2009", null)); Console.WriteLine("Microsoft Visual C++ 2013: " + IsSoftwareInstalled("Microsoft Visual C++ 2013", null)); Console.WriteLine("Microsoft Visual C++ 2012 Redistributable (x86): " + IsSoftwareInstalled("Microsoft Visual C++ 2013", ProgramVersion.x86)); Console.WriteLine("Microsoft Visual C++ 2012 Redistributable (x64): " + IsSoftwareInstalled("Microsoft Visual C++ 2013", ProgramVersion.x64)); Console.ReadKey(); } } }