【问题标题】:How to check if a software is installed or not in C#?如何检查C#中是否安装了软件?
【发布时间】:2011-12-01 14:18:04
【问题描述】:
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() 中的 String keyName 处崩溃,对象引用未设置为对象的实例。谁能告诉我是什么原因,

【问题讨论】:

  • 可能密钥不存在?
  • 除此之外,如果出现严重错误,卸载键的存在并不意味着它已安装...
  • 请不要将Wow6432Node 硬编码到您的应用程序中。如果您的目标是 .net 4,您可以使用 RegistryView 枚举打开注册表的 32 位或 64 位视图。
  • 这能回答你的问题吗? Check if application is installed in registry

标签: c# windows visual-studio-2010


【解决方案1】:

我认为问题可能出在这里:

key = Registry.LocalMachine.OpenSubKey(
          @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
foreach (String keyName in key.GetSubKeyNames())

在 32 位上,该密钥不存在。
所以你应该使用(对于你检查的每一个键)

key = ....
if (key != null) 
{
    foreach (String keyName in key.GetSubKeyNames())
    // ....
}

另一个信息:请注意,某些(很多?)注册表项不包含 displayName 值,因此您的比较可能会失败。如果不存在,请尝试(仅作为示例)使用键名代替 displayName

【讨论】:

    【解决方案2】:

    该错误意味着OpenSubKey 返回了null(当您尝试访问设置为null 的变量成员时,您将得到NullReferenceException)。这反过来意味着您要查找的注册表项不存在。

    在尝试使用key 对象之前添加null 检查。

    key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
    if(key != null)
    {
        foreach (String keyName in key.GetSubKeyNames())
        {
          // .... 
        }
    }
    

    【讨论】:

    • 我试过了,我运行我的代码,我已经安装了Skype,但是这个方法总是返回false
    • @Aqib:据我所知,并非您安装的每个软件都在注册表的该部分放置一个密钥...
    • 您询问了该错误,尽管您的标题与您的问题不同。从注册表读取时,需要确保运行程序的账户有从注册表读取的权限。
    猜你喜欢
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 2020-10-18
    • 2014-01-15
    相关资源
    最近更新 更多