Windows注册表的SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
密钥由一个密钥子集组成,每个密钥对应一个已安装的程序。每个程序的键名称都有一个明显随机的字符串,这使得定位给定程序的注册表键变得简单而容易(不是!)。注册表项的DisplayName
值包含程序的名称。我想使用LINQ构造一个字典(Dictionary<string, RegistryKey>
),对于每个注册表项,它将DisplayName
值映射到注册表项本身。有关如何做到这一点的任何建议吗?
与此同时,我将在LINQ上阅读,看看我是否能找到解决方案 - 这是我第一次真正考虑使用LINQ。我在这里发布我的问题,因为我认为这可能是一项适度的共同任务,因此其他人可能会受益。
提示:Microsoft.Win32.Registry
是用于访问Windows注册表的类。
答案 0 :(得分:2)
LINQ示例的一个很好的链接(没有双关语)
http://www.deepakkapoor.net/get-a-list-of-installed-applications-using-linq-and-c/
我会在你的情况下修改:
string registryKey =
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (Microsoft.Win32.RegistryKey key =
Registry.LocalMachine.OpenSubKey(registryKey))
{
(from a in key.GetSubKeyNames()
let r = key.OpenSubKey(a)
select new
{
DisplayName = r.GetValue("DisplayName"),
RegistryKey = r
})
.Where(c => c.DisplayName!= null)
.ToDictionary(k=>k.DisplayName, v=>v.RegistryKey);
}
答案 1 :(得分:0)
你可以在这些方面写点什么:
string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
var dict = new Dictionary<string, RegistryKey>();
using(var key = Registry.LocalMachine.OpenSubKey(registryKey))
{
foreach(string subkeyName in key.GetSubKeyNames())
{
using(var subkey = key.OpenSubKey(subkeyName))
{
dict.Add(subkey.GetValue("DisplayName"), subkey);
}
}
}
或者如果你想使用LINQ:
string registryKey =
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
var dict = new Dictionary<string, RegistryKey>();
using (var key = Registry.LocalMachine.OpenSubKey(registryKey))
{
var query = from a in
key.GetSubKeyNames()
let r = key.OpenSubKey(a)
select new
{
displayName = r.GetValue("DisplayName")
subKey = r
};
foreach (var item in query)
{
dict.Add(item.displayName, item.subKey);
}
}