Reputation: 37
Scenario : We do not care about Performance. We have an Array of Values that we need to check for in the registry. //The Values can have anything infront of them. We need to change this.
Right now, I don't feel like it itterates all the Values, it's like it skips a few and in the end becomes null and crashes.
This is a console Program, Main looks like this :
static void Main(string[] args)
{
string[] FASKeyWords = new string[] { "DummyValue1", "DummyValue2" };
RegistryKey BaseKey = Registry.LocalMachine;
foreach (string FASKeyWord in FASKeyWords)
{
Console.WriteLine("Looking in " + BaseKey.Name);
Console.WriteLine("Looking for : " + FASKeyWord);
GetSubKeys(BaseKey, FASKeyWord);
}
}
Now, Main calls this Void
private static void GetSubKeys(RegistryKey SubKey,string KeyWord)
{
foreach (string valueName in SubKey.GetValueNames())
{
string Value = SubKey.GetValue(valueName).ToString();
//Check for any values at all.
MessageBox.Show(valueName + " with data : " + Value);
if (Value.Contains(KeyWord))
MessageBox.Show("Found '" + KeyWord + "' At " + SubKey.Name);
}
foreach (string Key in SubKey.GetSubKeyNames())
{
MessageBox.Show(SubKey.Name);
GetSubKeys(SubKey.OpenSubKey(Key), KeyWord);
}
}
I'm far from the best working with the Registry class, but to my knowledge this should be okay right? , I've been starring blindly at it for far too long now, I figured it would be good with another pair of eyes :)
The Crash happens at : string Value = SubKey.GetValue(valueName).ToString(); with a NullReferenceException In Addition it does not Show a messageBox with all the Values inside a Key, It's like it just picks em out randomly.
A visual on the problem. http://peecee.dk/uploads/122012/Untitled2.png
Upvotes: 2
Views: 156
Reputation: 67090
From MSDN:
GetValue does not support reading values of type REG_NONE or REG_LINK. In both cases, the default value (null) is returned instead of the actual value.
You better to handle that situation (at least with a simple if
):
object RawValue = SubKey.GetValue(valueName);
string Value = RawValue != null ? RawValue.ToString() : "";
Upvotes: 2