Nash
Nash

Reputation: 113

How to search in a List by first item

Here is my list:

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

I'd like to know how to get the second p.Value of the p.Name after calling it:

hardDiskInfo(inputText.Text);

For example the value of the "FreeSpace" which is defined in Win32_LogicalDisk.

I'm having more Win32_ queries so knowing this will help me handling all of them and I'll be a happy panda.

Thank you.

Upvotes: 0

Views: 88

Answers (1)

Jeroen van Langen
Jeroen van Langen

Reputation: 22073

Are the Names unique?

You might try:

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

Next step: Use a Dictionary<string, string> which is much better.

Upvotes: 1

Related Questions