获取计算机内存信息

实现效果:

  

知识运用:

  ManagementObjectSearcher类的Get方法  //用于调用指定的WMI查询并返回结果集合

  public ManagementObjectCollection Get ()  //返回一个ManagementObjectCollection 包含匹配指定查询的对象

    PropertyData类的Value属性    //该类表是关于WMI属性的信息

  public Object Value {get; set;}  //Value属性用来获取或设置属性的当前值

实现代码:

        private void Form1_Load(object sender, EventArgs e)
        {
            InsertInfo("Win32_PhysicalMemory",ref listView1,true);  //将内存信息显示在列表中
        }

        public void InsertInfo(string Key,ref ListView lst, bool DontInsertNull)
        {
            listView1.Items.Clear();                                //清空ListView控件
                //创建ManagementObjectSearcher对象 使用其查找参数Key的内容
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from "+Key);
            try
            {
                //遍历foreach语句遍历ManagementObjectSearcher对象查找的内容
                foreach (ManagementObject share in searcher.Get())
                {
                    ListViewGroup grp;                              //创建一个ListViewGroup对象
                    try
                    {                                               //设置组标题
                        grp = lst.Groups.Add(share["Name"].ToString(), share["Name"].ToString());
                    }
                    catch (Exception)
                    {
                        grp = lst.Groups.Add(share.ToString(), share.ToString());
                    }
                    if (share.Properties.Count <= 0)                //如果没有找到信息 消息提示
                    {
                        MessageBox.Show("No Information Avable", "No Info", MessageBoxButtons.OK, 
                            MessageBoxIcon.Information);
                        return;
                    }
                    foreach (PropertyData data in share.Properties) //遍历获取到的数据
                    {
                        ListViewItem item = new ListViewItem(grp);
                        if (lst.Items.Count % 2 == 0)
                            item.BackColor = Color.Wheat;
                        else
                            item.BackColor = Color.WhiteSmoke;
                        item.Text = data.Name;                      //设置项目标题
                        if (data.Value != null && data.Value.ToString() != "")  //判断属性名是否为空
                        {
                            switch (data.Value.GetType().ToString())
                            {
                                case "System.String[]":
                                    string[] str = (string[])data.Value;//存储属性名
                                    string str2 = "";
                                    foreach (string s in str)
                                        str2 += s + " ";              //拆分记录
                                    item.SubItems.Add(str2);         //添加到ListView控件中
                                    break;
                                case "System.UInt16[]":
                                    ushort[] shr = (ushort[])data.Value;
                                    string ustr = "";
                                    foreach (ushort u in shr)
                                        ustr += u + " ";
                                    item.SubItems.Add(ustr);
                                    break;
                                default:
                                    item.SubItems.Add(data.Value.ToString()); //直接添加到ListView控件
                                    break;
                            }

                        }
                        else 
                        {
                            if (DontInsertNull == true)
                                item.SubItems.Add("没有信息");
                            else
                                continue;
                        }
                        lst.Items.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"ERROR",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

  

猜你喜欢

转载自www.cnblogs.com/feiyucha/p/10294384.html