首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LINQ toDictionary为空

LINQ toDictionary为空
EN

Stack Overflow用户
提问于 2013-06-05 20:37:39
回答 2查看 1.7K关注 0票数 0

有没有人能给我解释一下我做错了什么,为什么这个不起作用?我只是尝试从注册表项中获取值,并将它们作为字典返回给main函数。

代码语言:javascript
复制
    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;
    }

在检索字典之后,我将它绑定到一个列表框。

代码语言:javascript
复制
        listBox1.DisplayMember = "Value";
        listBox1.ValueMember = "Key";
        listBox1.DataSource = new BindingSource(u.ListPrograms(), null);

有没有更有效的方法来做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-05 20:41:34

你的代码

In Line (from a in key.GetSubKeyNames()

将其更改为

代码语言:javascript
复制
Apps = (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());

更新

下面是可用的代码

代码语言:javascript
复制
    public static Dictionary<string, string> ListPrograms()
    {
        Dictionary<string, string> Apps = new Dictionary<string, string>();
        string registryKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
        using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey))
        {
            if (key != null)
            {
                var key1 = key.GetSubKeyNames();
                foreach (var z in key1.Select(s => key.OpenSubKey(s))
                    .Where(b => b != null && b.GetValue("DisplayName") != null && b.GetValue("UninstallString") != null).Select(b => new
                    {
                        DisplayName = b.GetValue("DisplayName").ToString(),
                        RegistryKey = b.GetValue("UninstallString").ToString()
                    }).Where(z => !Apps.ContainsKey(z.RegistryKey)))
                {
                    Apps.Add(z.RegistryKey, z.DisplayName);
                }
            }
        }
        return Apps;
    }
票数 5
EN

Stack Overflow用户

发布于 2013-06-05 20:40:08

您永远不会影响您创建的字典和您返回的变量。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16940144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档