我在这里阅读这篇文章:Check if application is installed in registry
我非常喜欢这样的例子:Mroczny Arturek,但是我似乎无法找到实现此代码的最佳方法也返回应用程序安装路径。
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));
}
你会怎么推荐呢?
.Select(subkey => subkey.GetValue("InstallLocation") as string);
答案 0 :(得分:0)
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 void button1_Click(object sender, EventArgs e)
{
if (IsSoftwareInstalled("Type Program EXE Name HERE Without Using .exe", null))
{
var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall") ??
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
if (key == null)
{
return;
}
else
{
foreach (string subkey_name in key.GetSubKeyNames())
{
//MessageBox.Show(subkey_name); //Commented this out, i use this to iterate through and show me the install folder names.
using (RegistryKey subkey = key.OpenSubKey(subkey_name))
{
if (!object.ReferenceEquals(subkey.GetValue("DisplayName"), null))
{
string[] str = subkey.GetValueNames();
string SoftNames = Convert.ToString(subkey.GetValue("DisplayName"));
if (SoftNames == "Type HERE the DisPlayName")
{
string InstallLoc = Convert.ToString(subkey.GetValue("InstallLocation"));
//Strip the "double quotes"
string stripped = InstallLoc.Replace("\"", "");
MessageBox.Show(stripped + @"\Program.exe");
}
}
}
}
}
}
else
{
MessageBox.Show("Program not installed");
}
}
这将检查64/32是否在注册表中安装了程序,如果安装了该程序,它将返回所需的exe路径。