Sunday, March 6, 2011

Find match in subkey values

I'm having trouble with the following two items:

  • How to retrieve all of the subkey values in ClassesRoot\Typelib, and;
  • How to find a match for a known value (path/dll name) in the array of subkey values.

As background info, I'm trying to find a way to check if a DLL has been registered. Someone mentioned that checking ClassesRoot\Typelib for the DLL was one way of doing it, since I know the directory location and name of the DLL, but nothing else.

Does anyone have any tips? Cheers.

From stackoverflow
  • Have a look at Microsoft.Win32.Registry and Microsoft.Win32.RegistryKey.

    public void Foo()
    {
       foreach (string s in Microsoft.Win32.Registry.CurrentUser.GetSubKeyNames())
       {
          Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(s);
          // check here for the dll value and exit if found
          // recurse down the tree...
       }
    }
    
  • I've not tested it extensively and it has very little error handling code, but this should help you start.

    public static bool IsRegistered(string name, string dllPath)
    {
        RegistryKey typeLibKey = Registry.ClassesRoot.OpenSubKey("TypeLib");
        foreach (string libIdKeyName in typeLibKey.GetSubKeyNames())
        {
            RegistryKey libIdKey = typeLibKey.OpenSubKey(libIdKeyName);
            foreach (string versionKeyName in libIdKey.GetSubKeyNames())
            {
                RegistryKey versionKey = libIdKey.OpenSubKey(versionKeyName);
                string regName = (string)versionKey.GetValue("");
                if (regName == name)
                {
                    foreach (string itterKeyName in versionKey.GetSubKeyNames())
                    {
                        int throwawayint;
                        if (int.TryParse(itterKeyName, out throwawayint))
                        {
                            RegistryKey itterKey = versionKey.OpenSubKey(itterKeyName);
                            string regDllPath = (string)itterKey.OpenSubKey("win32").GetValue("");
                            if (regDllPath == dllPath)
                            {
                                return true;
                            }
                        }
                    }
                }
            }
        }
    
        return false;
    }
    

    }

0 comments:

Post a Comment