frmMain.cs
上传用户:amenzjw
上传日期:2022-06-29
资源大小:13k
文件大小:8k
源码类别:

系统编程

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.Management;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. namespace xlzHardwareInfo
  11. {
  12.     public partial class frmMain : Form
  13.     {
  14.         public frmMain()
  15.         {
  16.             InitializeComponent();
  17.             cmbxOption.SelectedItem = "Win32_Processor";
  18.         }
  19.         private void InsertInfo(string Key, ref ListView lst, bool DontInsertNull)
  20.         {
  21.             lst.Items.Clear();
  22.             ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key);
  23.             try
  24.             {
  25.                 foreach (ManagementObject share in searcher.Get())
  26.                 {
  27.                     ListViewGroup grp;
  28.                     try
  29.                     {
  30.                         grp = lst.Groups.Add(share["Name"].ToString(), share["Name"].ToString());
  31.                     }
  32.                     catch
  33.                     {
  34.                         grp = lst.Groups.Add(share.ToString(), share.ToString());
  35.                     }
  36.                     if (share.Properties.Count <= 0)
  37.                     {
  38.                         MessageBox.Show("No Information Available", "No Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
  39.                         return;
  40.                     }
  41.                     foreach (PropertyData PC in share.Properties)
  42.                     {
  43.                         ListViewItem item = new ListViewItem(grp);
  44.                         if (lst.Items.Count % 2 != 0)
  45.                             item.BackColor = Color.White;
  46.                         else
  47.                             item.BackColor = Color.WhiteSmoke;
  48.                         item.Text = PC.Name;
  49.                         if (PC.Value != null && PC.Value.ToString() != "")
  50.                         {
  51.                             switch (PC.Value.GetType().ToString())
  52.                             {
  53.                                 case "System.String[]":
  54.                                     string[] str = (string[])PC.Value;
  55.                                     string str2 = "";
  56.                                     foreach (string st in str)
  57.                                         str2 += st + " ";
  58.                                     item.SubItems.Add(str2);
  59.                                     break;
  60.                                 case "System.UInt16[]":
  61.                                     ushort[] shortData = (ushort[])PC.Value;
  62.                                     string tstr2 = "";
  63.                                     foreach (ushort st in shortData)
  64.                                         tstr2 += st.ToString() + " ";
  65.                                     item.SubItems.Add(tstr2);
  66.                                     break;
  67.                                 default:
  68.                                     item.SubItems.Add(PC.Value.ToString());
  69.                                     break;
  70.                             }
  71.                         }
  72.                         else
  73.                         {
  74.                             if (!DontInsertNull)
  75.                                 item.SubItems.Add("No Information available");
  76.                             else
  77.                                 continue;
  78.                         }
  79.                         lst.Items.Add(item);
  80.                     }
  81.                 }
  82.             }
  83.             catch (Exception exp)
  84.             {
  85.                 MessageBox.Show("can't get data because of the followeing error n" + exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
  86.             }
  87.         }
  88.         private void RemoveNullValue(ref ListView lst)
  89.         {
  90.             foreach (ListViewItem item in lst.Items)
  91.                 if (item.SubItems[1].Text == "No Information available")
  92.                     item.Remove();
  93.         }
  94.         #region Control events ...
  95.         //private void cmbxNetwork_SelectedIndexChanged(object sender, EventArgs e)
  96.         //{
  97.         //    //InsertInfo(cmbxNetwork.SelectedItem.ToString(), ref lstNetwork, chkNetwork.Checked);
  98.         //}
  99.         //private void cmbxSystemInfo_SelectedIndexChanged(object sender, EventArgs e)
  100.         //{
  101.         //    //InsertInfo(cmbxSystemInfo.SelectedItem.ToString(), ref lstSystemInfo, chkSystemInfo.Checked);
  102.         //}
  103.         //private void cmbxUtility_SelectedIndexChanged(object sender, EventArgs e)
  104.         //{
  105.         //   // InsertInfo(cmbxUtility.SelectedItem.ToString(), ref lstUtility, chkUtility.Checked);
  106.         //}
  107.         //private void cmbxUserAccount_SelectedIndexChanged(object sender, EventArgs e)
  108.         //{
  109.         //    //InsertInfo(cmbxUserAccount.SelectedItem.ToString(), ref lstUserAccount, chkUserAccount.Checked);
  110.         //}
  111.         //private void cmbxStorage_SelectedIndexChanged(object sender, EventArgs e)
  112.         //{
  113.         //   // InsertInfo(cmbxStorage.SelectedItem.ToString(), ref lstStorage, chkDataStorage.Checked);
  114.         //}
  115.         //private void cmbxDeveloper_SelectedIndexChanged(object sender, EventArgs e)
  116.         //{
  117.         //    //InsertInfo(cmbxDeveloper.SelectedItem.ToString(), ref lstDeveloper, chkDeveloper.Checked);
  118.         //}
  119.         //private void cmbxMemory_SelectedIndexChanged(object sender, EventArgs e)
  120.         //{
  121.         //    //InsertInfo(cmbxMemory.SelectedItem.ToString(), ref lstMemory, chkMemory.Checked);
  122.         //}
  123.         private void chkHardware_CheckedChanged(object sender, EventArgs e)
  124.         {
  125.             if (chkHardware.Checked)
  126.                 RemoveNullValue(ref lstDisplayHardware);
  127.             else
  128.                 InsertInfo(cmbxOption.SelectedItem.ToString(), ref lstDisplayHardware, chkHardware.Checked);
  129.         }
  130.         private void cmbxOption_SelectedIndexChanged(object sender, EventArgs e)
  131.         {
  132.             InsertInfo(cmbxOption.SelectedItem.ToString(), ref lstDisplayHardware, chkHardware.Checked);
  133.         }
  134.         //private void chkDataStorage_CheckedChanged(object sender, EventArgs e)
  135.         //{
  136.         //    if (chkDataStorage.Checked)
  137.         //        RemoveNullValue(ref lstStorage);
  138.         //    else
  139.         //        InsertInfo(cmbxStorage.SelectedItem.ToString(), ref lstStorage, chkDataStorage.Checked);
  140.         //}
  141.         //private void chkMemory_CheckedChanged(object sender, EventArgs e)
  142.         //{
  143.         //    if (chkMemory.Checked)
  144.         //        RemoveNullValue(ref lstMemory);
  145.         //    else
  146.         //        InsertInfo(cmbxMemory.SelectedItem.ToString(), ref lstStorage, false);
  147.         //}
  148.         //private void chkSystemInfo_CheckedChanged(object sender, EventArgs e)
  149.         //{
  150.         //    if (chkSystemInfo.Checked)
  151.         //        RemoveNullValue(ref lstSystemInfo);
  152.         //    else
  153.         //        InsertInfo(cmbxSystemInfo.SelectedItem.ToString(), ref lstSystemInfo, false);
  154.         //}
  155.         //private void chkNetwork_CheckedChanged(object sender, EventArgs e)
  156.         //{
  157.         //    if (chkNetwork.Checked)
  158.         //        RemoveNullValue(ref lstNetwork);
  159.         //    else
  160.         //        InsertInfo(cmbxNetwork.SelectedItem.ToString(), ref lstNetwork, false);
  161.         //}
  162.         //private void chkUserAccount_CheckedChanged(object sender, EventArgs e)
  163.         //{
  164.         //    if (chkUserAccount.Checked)
  165.         //        RemoveNullValue(ref lstUserAccount);
  166.         //    else
  167.         //        InsertInfo(cmbxUserAccount.SelectedItem.ToString(), ref lstUserAccount, false);
  168.         //}
  169.         //private void chkDeveloper_CheckedChanged(object sender, EventArgs e)
  170.         //{
  171.         //    if (chkDeveloper.Checked)
  172.         //        RemoveNullValue(ref lstDeveloper);
  173.         //    else
  174.         //        InsertInfo(cmbxDeveloper.SelectedItem.ToString(), ref lstDeveloper, false);
  175.         //}
  176.         //private void chkUtility_CheckedChanged(object sender, EventArgs e)
  177.         //{
  178.         //    if (chkUtility.Checked)
  179.         //        RemoveNullValue(ref lstUtility);
  180.         //    else
  181.         //        InsertInfo(cmbxUtility.SelectedItem.ToString(), ref lstUtility, false);
  182.         //}
  183.         //private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  184.         //{
  185.         //    linkLabel1.LinkVisited = true;
  186.         //    System.Diagnostics.Process.Start("http://www.ShiraziOnline.net");
  187.         //}
  188.         #endregion
  189.        
  190.     }
  191. }