2008-12-15 61 views
1

我有以下兩個項目的麻煩:查找子項的值相匹配

  • 如何檢索所有ClassesRoot \類型庫子項的值,和;
  • 如何在子鍵值數組中找到已知值(路徑/ dll名稱)的匹配項。

作爲背景信息,我試圖找到一種方法來檢查DLL是否已被註冊。有人提到檢查DLL的ClassesRoot \ Typelib是這樣做的一種方式,因爲我知道DLL的目錄位置和名稱,但沒有別的。

有沒有人有任何提示?乾杯。

回答

2

我沒有廣泛地測試它,它的錯誤處理代碼很少,但是這應該可以幫助你開始。

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

}

1

看看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... 
    } 
}