Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

c# - Check if application is installed in registry

Right now I use this to list all the applications listed in the registry for 32bit & 64. I have seen the other examples of how to check if an application is installed without any luck.

string registryKey = @"SOFTWAREMicrosoftWindowsCurrentVersionUninstall";
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 = @"SOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall";
key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
    foreach (String a in key.GetSubKeyNames())
    {
        RegistryKey subkey = key.OpenSubKey(a);
        Console.WriteLine(subkey.GetValue("DisplayName"));
    }
}

So this snippet lists it all in the console window and what I am trying to do is just find one program title out of the list of display names to see if it's installed.

The last thing I tried was

if (subkey.Name.Contains("OpenSSL"))
    Console.Writeline("OpenSSL Found");
else
    Console.Writeline("OpenSSL Not Found");

Anything I tried came back either false or a false positive. Is there anyone that can show me how to just grab a title out of the list?

Please don't post up the well-known private static void IsApplicationInstalled(p_name) function. It does not work for me at all.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After searching and troubleshooting, I got it to work this way:

public static bool checkInstalled (string c_name)
{
    string displayName;

    string registryKey = @"SOFTWAREMicrosoftWindowsCurrentVersionUninstall";
    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 = @"SOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall";
    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;
}

And I simply just call it using

if(checkInstalled("Application Name"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...