2013-03-05 95 views
0

我在做一點計算機信息工具。現在我正在做用戶帳戶。我用可用用戶填充組合框。Win32_Account類奇怪的行爲

ManagementObjectSearcher mosAccounts = new ManagementObjectSearcher("SELECT * FROM Win32_Account"); 

     foreach (ManagementObject moUA in mosAccounts.Get()) 
     { 
      comboBox4.Items.Add(moUA["Name"].ToString()); 
     } 

然後我展示他們:

private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     ManagementObjectSearcher mosAccounts = new ManagementObjectSearcher("SELECT * FROM Win32_Account"); 

     foreach (ManagementObject moUA in mosAccounts.Get()) 
     { 
      lblDescriptionUA.Text = "Description: " + moUA["Description"].ToString(); 

      if (moUA["Domain"] != null) 
      { 
       lblDomain.Text = "Domain: " + moUA["Domain"].ToString(); 
      } 

      else 
       lblDomain.Text = "Domain: Not Available"; 

      if (moUA["InstallDate"] != null) 
      { 
       lblInstallDateUA.Text = "Install Date: " + moUA["InstallDate"].ToString(); 
      } 

      else 
       lblInstallDateUA.Text = "Install Date: Not Available"; 

      lblLocalAccount.Text = "Local Account: " + moUA["LocalAccount"].ToString(); 

      if (moUA["SID"] != null) 
      { 
       lblSID.Text = "SID: " + moUA["SID"].ToString(); 
      } 

      else 
       lblSID.Text = "SID: Not Available"; 

      if (moUA["SIDType"] != null) 
      { 
       lblSIDType.Text = "SID Type: " + moUA["SIDType"]; 
      } 

      else 
       lblSIDType.Text = "SID Type: Not Available"; 

      if (moUA["Status"] != null) 
      { 
       lblStatusUA.Text = "Status: " + moUA["Status"].ToString(); 
      } 

      else 
       lblStatusUA.Text = "Status: Not Available"; 
     } 
    } 

但每次我從下拉框中選擇一個用戶,標籤要瘋了,表現出這麼多的信息,切換它。這就像他們正在顯示關於所有用戶的信息。有什麼我失蹤?

附加信息:我在工作。有很多域用戶。組合框向他們展示了他們。因爲所有的域用戶都是Mybe。我做了一個選項,只顯示本地用戶,但與此相同的問題。

回答

0

但是每次我從組合框中選擇一個用戶時,標籤都會變得瘋狂,顯示如此多的信息,將其切換。這就像他們正在顯示關於所有用戶的信息。

是 - 因爲這正是代碼說什麼做:

// Every time you select a user... 
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    // Find all the information about all users... 
    ManagementObjectSearcher mosAccounts = 
     new ManagementObjectSearcher("SELECT * FROM Win32_Account"); 

    // Iterate over all those users and display it 
    foreach (ManagementObject moUA in mosAccounts.Get()) 
    { 
     ... 
    } 
} 

代碼是做什麼你問它。現在,爲什麼你連接了一個這樣的事件處理程序?考慮一下當你選擇的索引改變時你想實現的實際上是。你真的想要找到單個選定的用戶嗎?如果是這樣,大概您只需要更改ManagementObjectSearcher中使用的查詢,然後找到單個結果而不是遍歷所有結果。