|
|
|
当前的位置 >> 返回首页 >> 营销方案 |
|
软件开发实现采集windows计算机硬件参数 |
|
发布者:西安软件公司 发布时间:2019/2/27 阅读:16次 |
|
实现思路: 通过 System.Management 下的 ManagementObjectSearcher 类,可以方便的读取到计算机硬件信息。 1.首先代码中引入命名空间: using System.Management;
2.实例化ManagementObjectSearcher类 ManagementObjectSearcher searcher = new ManagementObjectSearcher( "select * from " + Key);
3.上面的代码中的Key是用来读取数据时替换的变量。例如,为了获得CPU的信息,需要传入的key为:Win32_Processor。
4.关键代码:
private void InsertInfo(string key, ref ListView listView, bool dontInsertNull) { listView.Items.Clear(); var searcher = new ManagementObjectSearcher("select * from " + key); try { foreach (var o in searcher.Get()) { var share = (ManagementObject) o; ListViewGroup listViewGroup; try { listViewGroup = listView.Groups.Add(share["Name"].ToString(), share["Name"].ToString()); } catch { listViewGroup = listView.Groups.Add(share.ToString(), share.ToString()); } if (share.Properties.Count <= 0) { MessageBox.Show("No Information Available", "No Info", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } foreach (var data in share.Properties) { var item = new ListViewItem(listViewGroup); if (listView.Items.Count % 2 != 0) item.BackColor = Color.White; else item.BackColor = Color.WhiteSmoke; item.Text = data.Name; if (data.Value != null && data.Value.ToString() != "") { switch (data.Value.GetType().ToString()) { case "System.String[]": var str = (string[]) data.Value; var str2 = ""; foreach (var st in str) str2 += st + " "; item.SubItems.Add(str2); break; case "System.UInt16[]": var shortData = (ushort[]) data.Value; var tstr2 = ""; foreach (var st in shortData) tstr2 += st + " "; item.SubItems.Add(tstr2); break; default: item.SubItems.Add(data.Value.ToString()); break; } } else { if (!dontInsertNull) item.SubItems.Add("No Information available"); else continue; } listView.Items.Add(item); } } } catch (Exception exp) { MessageBox.Show("can’t get data because of the followeing error \n" + exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); }
|
|