1
我有以下两个项目的麻烦:查找子项的值相匹配
- 如何检索所有ClassesRoot \类型库子项的值,和;
- 如何在子键值数组中找到已知值(路径/ dll名称)的匹配项。
作为背景信息,我试图找到一种方法来检查DLL是否已被注册。有人提到检查DLL的ClassesRoot \ Typelib是这样做的一种方式,因为我知道DLL的目录位置和名称,但没有别的。
有没有人有任何提示?干杯。
我有以下两个项目的麻烦:查找子项的值相匹配
作为背景信息,我试图找到一种方法来检查DLL是否已被注册。有人提到检查DLL的ClassesRoot \ Typelib是这样做的一种方式,因为我知道DLL的目录位置和名称,但没有别的。
有没有人有任何提示?干杯。
我没有广泛地测试它,它的错误处理代码很少,但是这应该可以帮助你开始。
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;
}
}
看看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...
}
}