Sign in to follow this  

Registry Problem

This topic is 4518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

This topic is 4518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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