【问题标题】:Find match in subkey values在子键值中查找匹配
【发布时间】:2008-12-15 12:58:39
【问题描述】:

以下两项有问题:

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

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

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

【问题讨论】:

    标签: c# registry


    【解决方案1】:

    我没有对它进行广泛的测试,它的错误处理代码也很少,但这应该可以帮助你开始。

    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】:

      看看 Microsoft.Win32.Registry 和 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...
         }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-05-31
        • 2014-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多