c# - 检查应用程序是否安装在注册表中

标签 c# c windows registry

现在我用它来列出注册表中列出的所有 32 位和 64 位应用程序。 我已经看到了如何检查应用程序是否已安装的其他示例,但运气不佳。

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");

我尝试的任何结果要么是假的,要么是误报。有没有人可以告诉我如何从列表中获取标题?

请不要发布众所周知的 private static 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"))

关于c# - 检查应用程序是否安装在注册表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16379143/

相关文章:

c# - 如何在 Unity 3d 中跳转?

c - 是否有非致命的等价于 c 中的断言?

c - 在C中读取整数数组

c - 写入 16 位原始 PCM 文件时出现问题

.net - 什么是Windows编程以及它的发展方向?老实说,win32已经死了,不是吗? [复制]

windows - Regedit:从命令提示符查找和导出 key

c# - 选择 View 模型时如何在 IQueryable 中添加新的 where 条件?

c# - 如何在 C# 中获取修改后的文本框值?

windows - 是否可以通过编程方式禁用 Windows 上的 Caps Lock 键?

c# - 获取两个值之间的 n 个不同的随机数,其总和等于给定数