Search Array Arrays?

This applys to searching the registry for a 'value' much like the manual method. So we have an array of arrays to search, however I'm stumped. I need to find the Windows Installer 3.1 value (3.1.4000.24.35) in the XP or 2000 registry. I would rather conduct a wide search instead of a specific key search because the value location varies between OS's...........

RegistryKey rkey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Updates", false);
string[] skNames = rkey.GetSubKeyNames();
string[] skValues = rkey.GetValueNames();

   foreach (string sub in skNames)
   {
         foreach (string val in skValues)
         {
               listBox1.Items.Add(sub);
         }
    }
LVL 1
kvnsdrAsked:
Who is Participating?
 
c_myersCommented:
The code I posted will get you most of the way there.

You'll need one wrapper function to kick it all off, though:

public bool FindValInKey( string keyPath, string valToFind )
{
   string foundKeyPath = null;
   string foundValName = null;
         
   using( RegistryKey rootKey = Registry.LocalMachine.OpenSubKey(keyPath, false) )
   {
      FindValueRecursive( rootKey, valToFind, out foundKeyPath, out foundValName );
   }

   return ( foundKeyPath != null );
}


Then, from wherever, you'd call it something like this:

if( FindValInKey("Software\\Microsoft\\Updates", "3.1.4000.24.35" ) )
{
   MessageBox.Show("Found key!");
}
0
 
c_myersCommented:
I'm not sure *exactly* what you're trying to do, but what I THINK you're trying to do is recursively look through the tree under a specific reg key (HKLM\Software\Microsoft\Updates in this case) for a specific key/value pair, right?

Recursion is your friend here.

Here's a simple console app that searches the key you mentioned above, recursively, until it finds a value with that value, or not.

==== C# EXAMPLE ====
using System;
using Microsoft.Win32;

public class TestConsole
{
      public static void Main( string[] args )
      {
            string foundKeyPath = null;
            string foundValName = null;
            
            using( RegistryKey rootKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Updates", false) )
            {
                FindValueRecursive( rootKey, "3.1.4000.24.35", out foundKeyPath, out foundValName );
            }
            
            if( foundKeyPath != null )
            {
                  // TODO: We found it! now what?
                  Console.WriteLine("Key: {0}", foundKeyPath);
                  Console.WriteLine("Value: {0}", foundValName);
                  Console.WriteLine("We found it! Now what?");
            }
            else
            {
                  Console.WriteLine("Not found.");
            }
      }

      private static void FindValueRecursive( RegistryKey curKey, string valToFind, out string keyPath, out string valueName )
      {
            keyPath = null;
            valueName = null;
            
            // First look for values in this key
            foreach( string curValName in curKey.GetValueNames() )
            {
                  object curVal = curKey.GetValue(curValName );
                  
                  if( curVal != null && curVal.Equals( valToFind) )
                  {
                        keyPath = curKey.Name;
                        valueName = curValName;
                        return; // We found it!
                  }
            }
                  
            // If not found, look in my sub-keys
            foreach( string subKeyName in curKey.GetSubKeyNames() )
            {
                  using( RegistryKey subKey = curKey.OpenSubKey( subKeyName, false ) )
                  {
                        FindValueRecursive( subKey, valToFind, out keyPath, out valueName );
                  
                        if( keyPath != null )
                              return; // We found it!
                  }
            }
            
            // Nothin' found, give up
            return;
      }
}

==== END C# EXAMPLE ====
0
 
kvnsdrAuthor Commented:
Excellent!

I would like to use that code as a seperate class, passing a parameter to it like "3.1.4000.24.35" from the Form1 main class. If it matches, then I might recieve a bool or something else back to the Form1......
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.