Sign in to follow this  
Kryptus

Registry Problem

Recommended Posts

I have an XML file with a list of games in it. What I have done (and trying to do) is have my program scan for those games on the computer by checking the registry. I am using C#. I use the code below:
// My registry key object
RegistryKey myKey = Registry.LocalMachine;
// Get the registry key from the XML file and open it
myKey.OpenSubKey(game.GetAttribute("location"), false);

The last line of code here is in a loop, and it keeps looping until it reaches the end of the list. Let's say for example, I don't have Warcraft 3 installed but that game is in the list. It's gonna open the sub key. The problem is this code just loads the key regardless of it's existence!! Is there some way of detecting if a registry key exists? I tried the following code but it didn't work. I got a "Object reference not set to instance of object" error:
foreach (XmlElement game in games)
            {
                try
                {
                    myKey.OpenSubKey(game.GetAttribute("location"), false);

                    if (myKey.GetValue(game.GetAttribute("value")).ToString() == null)
                    {
                        //
                    }
                    else
                    {
                        // Add the game to the list
                        lvGames.Items.Add(game.GetAttribute("name"));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

And the XML loading code is fine. So don't worry about the XML part. I think the error lies with the IF statement ???

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this