?
Solved

Search Array Arrays?

Posted on 2006-03-29
3
Medium Priority
?
180 Views
Last Modified: 2010-04-16
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);
         }
    }
0
Comment
Question by:kvnsdr
  • 2
3 Comments
 
LVL 4

Expert Comment

by:c_myers
ID: 16329535
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
 
LVL 1

Author Comment

by:kvnsdr
ID: 16337011
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
 
LVL 4

Accepted Solution

by:
c_myers earned 2000 total points
ID: 16339393
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question