首页 新闻 会员 周边 捐助

winform中获取硬盘和cpu物理序列号

0
悬赏园豆:5 [待解决问题]

/// <summary>
  /// 1.获取CPU序列号代码
  /// </summary>
  public static string GetCpuID()
  {
   try
   {
    string cpuInfo = string.Empty;//cpu序列号
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
    foreach (ManagementObject mo in searcher.Get())
    {
     cpuInfo = mo.Properties["ProcessorId"].Value.ToString().Trim();
     break;
    }
    if (string.IsNullOrEmpty(cpuInfo))
     cpuInfo = "unknow";
    return cpuInfo;
   }
   catch(Exception ex)
   {
    return "unknow";
   }

  }

 

/// <summary>
  /// 3.获取硬盘ID
  /// </summary>
  public static string GetDiskID()
  {

   //取第一块硬盘编号
   try
   {
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
    string strHardDiskID = string.Empty;
    foreach (ManagementObject mo in searcher.Get())
    {
     strHardDiskID = mo.Properties["SerialNumber"].Value.ToString().Trim();
     break;
    }
    if (string.IsNullOrEmpty(strHardDiskID))
     strHardDiskID = "unknow";
    return strHardDiskID;
   }
   catch (Exception ex)
   {
    return "unknow";
   }

}

 

我做测试 一个机器上获取不到cpu序列号  另外一个机器上获取不到硬盘物理序列号,我在网上看了一下 ,基本上都是转贴的  内容都是一样的 ,没有找到说是有这种问题的 ,还请知道怎么做的高手帮忙看一下  小弟不胜感激!

轻清风的主页 轻清风 | 初学一级 | 园豆:200
提问于:2010-09-09 10:27
< >
分享
所有回答(1)
0

获取CPUID

public static  string FindComputerCPU_ID()

{

ManagementScope ms = new ManagementScope("root\\cimv2");
            ms.Connect();
            ManagementObjectSearcher sysinfo = new ManagementObjectSearcher(ms, new SelectQuery("Win32_Processor"));
            string cpuId = "";
            foreach (ManagementObject sys in sysinfo.Get())
            {
                cpuId = sys["ProcessorId"].ToString();
            }

    return cpuId;

}

love_99 | 园豆:325 (菜鸟二级) | 2010-09-09 13:20
获取硬盘ID public static string GetComputerDisk_ID() { ManagementScope ms = new ManagementScope("root\\cimv2"); ms.Connect(); ManagementObjectSearcher sysInfo = new ManagementObjectSearcher(ms, new SelectQuery("Win32_PhysicalMedia")); string diskId = string.Empty; foreach (ManagementObject sys in sysInfo.Get()) { diskId = sys["SerialNumber"].ToString(); } return diskId; }
支持(0) 反对(0) love_99 | 园豆:325 (菜鸟二级) | 2010-09-09 13:27
兄弟 感谢你的回答 不过你的方法好像并没有解决问题, 和我贴出来的代码是一样的效果 sys["ProcessorId"]所获取的值为null 所以会报“未将对象引用设置到对象的实例”
支持(0) 反对(0) 轻清风 | 园豆:200 (初学一级) | 2010-09-09 13:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册