代码之家  ›  专栏  ›  技术社区  ›  Rowan

在子项值中查找匹配项

  •  1
  • Rowan  · 技术社区  · 17 年前

    我在以下两个项目上遇到了问题:

    • 如何检索ClassesRoot\Typelib中的所有子键值,以及;
    • 如何在子项值数组中找到已知值(路径/dll名称)的匹配项。

    作为背景信息,我试图找到一种方法来检查DLL是否已注册。有人提到,检查DLL的ClassesRoot\Typelib是一种方法,因为我知道DLL的目录位置和名称,但别的什么都不知道。

    有人有什么建议吗?干杯。

    2 回复  |  直到 17 年前
        1
  •  2
  •   ICR    17 年前

    我还没有对它进行广泛的测试,它几乎没有错误处理代码,但这应该能帮助你开始。

    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;
    }
    

    }

        2
  •  1
  •   WOPR    17 年前

    看看微软。Win32.Registry和微软。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...
       }
    }