Original Post
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: 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: And the XML loading code is fine. So don't worry about the XML part. I think the error lies with the IF statement ???
// 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);
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);
}
}