如何通过第一项

时间:2016-11-07 15:45:52

标签: c# list search

这是我的清单:

    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);

例如&#34; FreeSpace&#34;的价值在Win32_LogicalDisk中定义。

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

谢谢。

1 个答案:

答案 0 :(得分: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<string, string>