详解如何使用C#获取计算机信息
更新时间:2024年10月21日 11:53:42 作者:天天代码码天天
这篇文章主要为大家详细介绍了如何使用C#实现获取计算机信息,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下
效果
项目
代码
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 获取计算机信息 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 获取计算机硬件信息 /// </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"); // 获取机器名 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(); } } }
到此这篇关于详解如何使用C#获取计算机信息的文章就介绍到这了,更多相关C#获取计算机信息内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
最新评论