詳解如何使用C#獲取計(jì)算機(jī)信息
更新時間:2024年10月21日 11:53:42 作者:天天代碼碼天天
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)獲取計(jì)算機(jī)信息,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以參考下
效果
項(xiàng)目
代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Management; namespace 獲取計(jì)算機(jī)信息 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 獲取計(jì)算機(jī)硬件信息 /// </summary> /// <param name="path">部件</param> /// <param name="key">鍵值</param> /// <returns></returns> public string GetComputerHardWareInfo(string path, string key) { try { ManagementClass managementClass = new ManagementClass(path); ManagementObjectCollection moc = managementClass.GetInstances(); PropertyDataCollection properties = managementClass.Properties; foreach (var property in properties) { if (property.Name == key) { foreach (var mo in moc) { return mo.Properties[property.Name].Value.ToString(); } } } } catch (Exception ex) { //記錄異常信息 } return string.Empty; } private void button1_Click(object sender, EventArgs e) { //獲取CPU ID string CPU_ID = GetComputerHardWareInfo("Win32_Processor", "ProcessorId"); //獲取主板序列號 string Board_SN = GetComputerHardWareInfo("Win32_BaseBoard", "SerialNumber"); //獲取硬盤序列號 string Disk_SN = GetComputerHardWareInfo("Win32_DiskDrive", "Model"); string UUID = GetComputerHardWareInfo("Win32_ComputerSystemProduct", "UUID"); // 獲取機(jī)器名 string MachineName = Environment.MachineName; string OSVersion = Environment.OSVersion.VersionString.ToString(); string UserName = Environment.UserName; string OperatingSystem = GetComputerHardWareInfo("Win32_OperatingSystem", "Caption"); StringBuilder sb = new StringBuilder(); sb.AppendLine(string.Format("CPU_ID:{0}", CPU_ID)); sb.AppendLine(string.Format("Board_SN:{0}", Board_SN)); sb.AppendLine(string.Format("Disk_SN:{0}", Disk_SN)); sb.AppendLine(string.Format("UUID:{0}", UUID)); sb.AppendLine(string.Format("MachineName:{0}", MachineName)); sb.AppendLine(string.Format("OSVersion:{0}", OSVersion)); sb.AppendLine(string.Format("UserName:{0}", UserName)); sb.AppendLine(string.Format("OperatingSystem:{0}", OperatingSystem)); textBox1.Text = sb.ToString(); } } }
到此這篇關(guān)于詳解如何使用C#獲取計(jì)算機(jī)信息的文章就介紹到這了,更多相關(guān)C#獲取計(jì)算機(jī)信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用SignalR實(shí)現(xiàn)與前端vue實(shí)時通信的示例代碼
SignalR 是 ASP.NET Core 的一個庫,它簡化了在應(yīng)用程序中添加實(shí)時通信的過程,無論是聊天應(yīng)用、實(shí)時游戲還是協(xié)作工具,SignalR 都能提供高效且易于實(shí)現(xiàn)的解決方案,本文給大家介紹了C#使用SignalR實(shí)現(xiàn)與前端vue實(shí)時通信的實(shí)現(xiàn),需要的朋友可以參考下2024-10-10C#利用GDI+給圖片添加文字(文字自適應(yīng)矩形區(qū)域)
這篇文章主要給大家介紹了關(guān)于C#利用GDI+給圖片添加文字(文字自適應(yīng)矩形區(qū)域)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2018-04-04C#實(shí)現(xiàn)人民幣大寫轉(zhuǎn)換示例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)人民幣大寫轉(zhuǎn)換,需要的朋友可以參考使用2013-12-12DevExpress實(shí)現(xiàn)TreeList父子節(jié)點(diǎn)CheckState狀態(tài)同步的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)TreeList父子節(jié)點(diǎn)CheckState狀態(tài)同步的方法,需要的朋友可以參考下2014-08-08