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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 ====
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......
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!");
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.