Skip to main content
gamedev.net
🔒 Locked

Registry Problem

Started by Kryptus Aug 3, 2005 at 9:53 AM 0 replies 400+ views
Original Post
Kryptus
Kryptus
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 ???

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.