|
|
|
當前的位置 >> 返回首頁 >> 營銷方案 |
|
軟件開發(fā)實現(xiàn)采集windows計算機硬件參數(shù) |
|
發(fā)布者:西安軟件公司 發(fā)布時間:2019/2/27 閱讀:16次 |
|
實現(xiàn)思路: 通過 System.Management 下的 ManagementObjectSearcher 類,可以方便的讀取到計算機硬件信息。 1.首先代碼中引入命名空間: using System.Management;
2.實例化ManagementObjectSearcher類 ManagementObjectSearcher searcher = new ManagementObjectSearcher( "select * from " + Key);
3.上面的代碼中的Key是用來讀取數(shù)據(jù)時替換的變量。例如,為了獲得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); }
|
|