public static bool IsApplictionInstalled(string p_name)
{
string displayName;
RegistryKey key;
// search in: CurrentUser
key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
foreach (String keyName in key.GetSubKeyNames())
{
RegistryKey subkey = key.OpenSubKey(keyName);
displayName = subkey.GetValue("DisplayName") as string;
if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
{
return true;
}
}
// search in: LocalMachine_32
key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
foreach (String keyName in key.GetSubKeyNames())
{
RegistryKey subkey = key.OpenSubKey(keyName);
displayName = subkey.GetValue("DisplayName") as string;
if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
{
return true;
}
}
// search in: LocalMachine_64
key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
foreach (String keyName in key.GetSubKeyNames())
{
RegistryKey subkey = key.OpenSubKey(keyName);
displayName = subkey.GetValue("DisplayName") as string;
if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
{
return true;
}
}
// NOT FOUND
return false;
}
該方法檢查32位或64位Win OS中的軟件,但其無法正常工作,在key.GetSubKeyNames()中的字符串keyName處粉碎,未將對象引用設置爲對象的實例。任何一個可以告訴我是什麼原因,如何檢查軟件是否安裝在C#中?
也許鍵不存在? – ChrisBint
除此之外,如果事情已經gobe可怕的錯誤,卸載鍵是否存在等,並不意味着它的安裝... –
請永遠不要硬編碼'Wow6432Node'到應用程序中。如果您的目標是.net 4,則可以使用RegistryView'枚舉打開註冊表的32位或64位視圖。 –