C#中获取cpu序列号,硬盘id,网卡mac地址

    有时需要在C#中获取计算机的一些硬件设备信息,如CPU序列号、硬盘ID等,.NET 提供的System.Management.ManagementClass是对设备管理的支持,其表示公共信息模型 (CIM) 管理类。管理类是一个 WMI 类,如 Win32_LogicalDisk 和 Win32_Process,前者表示磁盘驱动器,后者表示进程(如 Notepad.exe)。通过该类的成员,可以使用特定的 WMI 类路径访问 WMI 数据。有关更多信息,请参见“Windows Management Instrumentation”文档中的“Win32 Classes”(Win32 类),该文档位于 http://www.microsoft.com/china/msdn/library 上的 MSDN Library 中。 

    //获取cpu序列号  
    string cpuInfo = "";
    ManagementClass cimobject = new ManagementClass("Win32_Processor"); 
    ManagementObjectCollection moc = cimobject.GetInstances(); 
    foreach(ManagementObject mo in moc) 
    { 
     if(mo.Properties["ProcessorId"].Value != null)
      cpuInfo += mo.Properties["ProcessorId"].Value.ToString(); 
    } 
    result += cpuInfo;

    //获取硬盘ID 
    string HDid = ""; 
    ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive"); 
    ManagementObjectCollection moc1 = cimobject1.GetInstances(); 
    foreach(ManagementObject mo in moc1) 
    { 
     if(mo.Properties["Model"].Value != null)
      HDid += (string)mo.Properties["Model"].Value.ToString();  
    } 
    result += HDid;

    //获取网卡硬件地址     
    string MACAddress = "";
    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
    ManagementObjectCollection moc2 = mc.GetInstances(); 
    foreach(ManagementObject mo in moc2) 
    { 
     if((bool)mo["IPEnabled"] == true && mo["MacAddress"] != null) 
      MACAddress += mo["MacAddress"].ToString(); 
     mo.Dispose(); 
    } 
    result += MACAddress;

转载于:https://www.cnblogs.com/mywebname/archive/2007/11/21/966974.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值