获取计算机内存信息
2023-09-14 08:58:58 时间
实现效果:
知识运用:
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); } }
相关文章
- 增加内存会让计算机变快吗?
- 从进程栈内存底层原理到Segmentation fault报错
- Android为何比iOS卡论1G内存的使用详解手机开发
- Java直接(堆外)内存使用详解编程语言
- 信息探索Linux系统内存与CPU性能信息(查linux内存cpu)
- Linux内存使用情况分析(linux内存占用情况)
- Linux系统优化之内存释放脚本(linux内存释放脚本)
- 页面缓存:内存和文件之间的那些事
- 如何处理Linux内存占满问题?(linux内存占满)
- 轻松掌握Linux内存查看技巧(linux 内存查看)
- 提升Linux内存使用率的关键方法(linux 内存 使用率)
- 内存探索Redis的可用内存之路(查询redis 所有可用)
- 动态增长的内存空间给Redis带来更多(内存 redis)
- IEJS编程需注意的内存释放问题
- mysql数据库中my.ini的优化2G内存针对站多抗压型的设置