【问题标题】:How to search in a List by first item如何按第一项在列表中搜索
【发布时间】:2016-11-07 15:45:51
【问题描述】:

这是我的清单:

    public static List<Tuple<string, string>> hardDiskInfo(string hostname)
    {
        var hardDiskInfo = new List<Tuple<string, string>>();
        ManagementScope Scope;
        if (!hostname.Equals("localhost", StringComparison.OrdinalIgnoreCase))
        {
            ConnectionOptions Conn = new ConnectionOptions();
            Conn.Username = Properties.Settings.Default.uName;
            Conn.Password = Properties.Settings.Default.pWord;
            Conn.Authority = "ntlmdomain:" + Properties.Settings.Default.doMain;
            Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", hostname), Conn);
        }
        else
            Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", hostname), null);
        Scope.Connect();
        ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3 OR DriveType = 4");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(Scope, query);
        ManagementObjectCollection queryCollection = searcher.Get();
       foreach (ManagementObject mo in queryCollection)
        {
            foreach (PropertyData p in mo.Properties)
            {
                if (p.Value != null)
                {
                    hardDiskInfo.Add(new Tuple<string, string>(p.Name.ToString(), p.Value.ToString()));
                }
            }
        }
        return hardDiskInfo;
    }

我想知道调用后如何获取 p.Name 的第二个 p.Value:

hardDiskInfo(inputText.Text);

例如Win32_LogicalDisk中定义的“FreeSpace”的值。

我有更多 Win32_ 查询,所以知道这将帮助我处理所有这些查询,我会成为一只快乐的熊猫。

谢谢。

【问题讨论】:

  • 我想知道调用后p.Name的第二个p.Value怎么获取:没有第二个值?
  • 好吧好吧,我的错。所以我想知道如何获取 p.Name 的 p.Value。
  • 它看起来像一个 NameValue 集合,所以p 有一个Name 和一个Value。意思是,Name 它自己是名称的值,而 de Value 包含该值。我可能不明白斗争是什么。
  • 困难在于我不知道该怎么做。我可以请那一行解释如何做吗?

标签: c# list search


【解决方案1】:

名称是否唯一?

你可以试试:

var values = hardDiskInfo(inputText.Text);

// Get the first or default which matches "FreeSpace".
var freeSpaceInfo = values.FirstOrDefault(item => item.Item1 == "FreeSpace");

// If it was found,
if(freeSpaceInfo != null)
{
    MessageBox.Show($"FreeSpace: {freeSpaceInfo.Item2}");
}

下一步:使用更好的Dictionary&lt;string, string&gt;。

【讨论】:

  • 非常感谢。太棒了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
  • 2021-12-16
  • 2011-08-21
  • 1970-01-01
相关资源
最近更新 更多