有人可以向我解释我做错了什么以及为什么这不起作用吗?我只是想从注册表项中获取值并将它们作为字典返回给主函数。
public Dictionary<string, string> ListPrograms()
{
///List<object> Apps = new List<object>();
Dictionary<string, string> Apps = new Dictionary<string, string>();
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.GetValue("UninstallString")
})
.Distinct()
.OrderBy(c => c.DisplayName)
.Where(c => c.DisplayName != null && c.RegistryKey != null)
.ToDictionary(k => k.RegistryKey.ToString(), v => v.DisplayName.ToString());
}
return Apps;
}
检索字典后,我将其绑定到列表框。
listBox1.DisplayMember = "Value";
listBox1.ValueMember = "Key";
listBox1.DataSource = new BindingSource(u.ListPrograms(), null);
但是,我的列表框总是空的。有没有更有效的方法来做到这一点?