0

I am creating a basic piece of software that I want to check the local machine for specific pieces of software and then check a box to indicate that the software is on the machine. I went through multiple thread and found a nice bit of code that was short and sweet. However, I am not able to link up the return value or true or false with my checkbox. Can someone tell me if I am using this code correctly? Essentially I want to check the list of items available in uninstall under a few different parameters(to cover my bases with 32 and 64 bit OS) And in this case I am searching for a piece of software called Symantec Encryption.

 public static bool IsApplictionInstalled(string PGP)
    {
        string keyName;

        // search in: CurrentUser
        keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        if (ExistsInSubKey(Registry.CurrentUser, keyName, "Symantec Encryption", PGP) == true)
        {
            return true;
        }

        // search in: LocalMachine_32
        keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        if (ExistsInSubKey(Registry.LocalMachine, keyName, "Symantec Encryption", PGP) == true)
        {
            return true;
        }

        // search in: LocalMachine_64
        keyName = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
        if (ExistsInSubKey(Registry.LocalMachine, keyName, "Symantec Encryption", PGP) == true)
        {
            return true;
        }

        return false;
    }


    public static bool ExistsInSubKey(RegistryKey p_root, string p_subKeyName, string p_attributeName, string PGP)
    {
        RegistryKey subkey;
        string displayName;

        using (RegistryKey key = p_root.OpenSubKey(p_subKeyName))
        {
            if (key != null)
            {
                foreach (string kn in key.GetSubKeyNames())
                {
                    using (subkey = key.OpenSubKey(kn))
                    {
                        displayName = subkey.GetValue(p_attributeName) as string;
                        if (PGP.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
                        {
                            return true;

                        }
                    }
                }
            }
        }
        return false;
    }

    public void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (PGP == true)
        {
            checkBox1.Checked = true;
        }
    }
    // ------------------------------------------------------------------------------END------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------END------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------END------------------------------------------------------------------------------------------------------
Joe Pearson
  • 105
  • 10

1 Answers1

-1

You're not even close. You need to call IsApplicationInstalled, and use the return value of that function.

public void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    checkBox1.Checked = IsApplicationInstalled(PGP);
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • i get an error with this route. name does not exist in current context. – Joe Pearson May 18 '15 at 19:29
  • 1
    @JoePearson Replace `"Symantec Encryption"` with your `PGP` variable in this code. – Der Kommissar May 18 '15 at 19:31
  • Okay I think I have it down correctly then. However, just so I know I'm not crazy. the "Symentec Encryption" is a string that is being searched in the regkey correct? and if it is present and any of the three queries then it returns the var PGP as true? – Joe Pearson May 18 '15 at 19:38
  • It checks various locations in the registry to try and find a key named *Symantec Encryption*, and then tries to match that key's value with whatever's in the *PGP* variable. It returns true if it can find a match for the *PGP* variable content in any one of the keys, false if all three fail. In the future, I strongly suggest that you learn what code you find on the internet does **before** you try using it. – Ken White May 18 '15 at 20:03
  • @Ken White, as much as I appreciate your feedback, I was correct in my understanding of the code above. Please take the time to understand that before commenting. I'm sorry that C# is not my main language, I am trying to piece together a simple script for a project and don't have the time to refresh a very old and rudimentary understanding of the language just for this project. – Joe Pearson May 18 '15 at 21:02