目录
效果
项目
代码
下载
效果
项目
代码
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();
}
}
}
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();
}
}
}
下载
源码下载