您现在的位置是: 首页 > .NET开发 > 文章详情 文章详情
C#获取当前主机硬件信息
2019-08-19 【.NET开发】 1377人浏览
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.Threading.Tasks;
-
using System.Net;
-
using System.Management; //在项目-》添加引用....里面引用System.Management
-
using System.Runtime.InteropServices;
-
namespace FileSplit
-
{
-
//此类用于获取当前主机的相关信息
-
public class MachineInfo
-
{
-
//用法示例
-
private string example()
-
{
-
string Info = "";
-
MachineInfo info = MachineInfo.I(); //获取主机的对象信息
-
Info = "\r\n主机名:" + Dns.GetHostName(); //获取主机名称
-
Info += "\r\n系统类型:" + info.GetSystemType();
-
Info += "\r\n物理内存:" + info.GetPhysicalMemory();
-
Info += "\r\n\r\n本地IP:";
-
string[] LocalIp = info.GetLocalIpAddress();
-
foreach (string ip in LocalIp) Info += "\r\n" + ip;
-
Info += "\r\n\r\n外网IP:";
-
string[] ExternalIp = info.GetExtenalIpAddress();
-
foreach (string ip in ExternalIp) Info += "\r\n" + ip;
-
Info += "\r\n\r\nMAC:";
-
Info += "\r\n" + info.GetNetCardMACAddress();
-
//Info += "\r\n" + info.GetMacAddress(LocalIp[0]);
-
Info += "\r\n";
-
Info += "\r\nCPU序列号:" + info.GetCPUSerialNumber();
-
//Info += "\r\nCPU编号:" + info.GetCPUID();
-
//Info += "\r\nCPU版本信息:" + info.GetCPUVersion();
-
//Info += "\r\nCPU名称信息:" + info.GetCPUName();
-
//Info += "\r\nCPU制造厂商:" + info.GetCPUManufacturer();
-
//Info += "\r\n主板制造厂商:" + info.GetBoardManufacturer();
-
//Info += "\r\n主板序列号:" + info.GetBIOSSerialNumber();
-
//Info += "\r\n硬盘序列号:" + info.GetHardDiskSerialNumber();
-
//Info += "\r\n显卡PNPDeviceID:\r\n" + info.GetVideoPNPID();
-
//Info += "\r\n声卡PNPDeviceID:\r\n" + info.GetSoundPNPID();
-
return Info;
-
}
-
static MachineInfo Instance;
-
///
-
/// 获取当前类对象的一个实例
-
///
-
public static MachineInfo I()
-
{
-
if (Instance == null) Instance = new MachineInfo();
-
return Instance;
-
}
-
///
-
/// 获取本地ip地址,多个ip
-
///
-
public String[] GetLocalIpAddress()
-
{
-
string hostName = Dns.GetHostName(); //获取主机名称
-
IPAddress[] addresses = Dns.GetHostAddresses(hostName); //解析主机IP地址
-
string[] IP = new string[addresses.Length]; //转换为字符串形式
-
for (int i = 0; i