2016-11-07 24 views
0

這裏搜索是我的清單:如何在列表的第一項

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_查詢,所以知道這將幫助我處理所有這些問題,我將成爲一個快樂的熊貓。

謝謝。

+0

_我想知道如何獲得p.Name的第二個p.Value後調用它:_沒有第二個值? –

+0

好吧,我的壞。所以我想知道如何獲得p.Name的p.Value。 – Nash

+0

它看起來像一個NameValue集合,所以'p'有一個'Name'和一個'Value'。意思是,'Name'它自身就是名稱的值,de'Value'包含這個值。我可能不明白這些鬥爭是什麼。 –

回答

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>這是好多了。

+0

非常感謝。這很棒。 – Nash

相關問題