熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

c#如何獲得cpu,硬盤的物理序列號

2013-11-13 10:28:19  來源: .NET編程 

  簡介這是c#如何獲得cpu硬盤的物理序列號的詳細頁面介紹了和c/c++有關的知識加入收藏請按鍵盤ctrl+D謝謝大家的觀看!要查看更多有關信息

  using system;

  using systemruntimeinteropservices;

  using  systemmanagement;

  namespace hardware

  {

  /// <summary>

  /// hardware_mac 的摘要說明

  /// </summary>

  public class hardwareinfo

  {

  //取機器名

  public string gethostname()

  {

  return dnsgethostname();

  }

  //取cpu編號

  public string getcpuid()

  {

  try

  {

  managementclass mc = new managementclass(win_processor);

  managementobjectcollection moc = mcgetinstances();

  string strcpuid = null ;

  foreach( managementobject mo in moc )

  {

  strcpuid = moproperties[processorid]valuetostring();

  break;

  }

  return strcpuid;

  }

  catch

  {

  return ;

  }

  }//end method

  //獲取cpu序列號

  public string getcpuid()

  {

  try

  {

  SystemManagementManagementObjectSearcher   Wmi   =   new   ManagementObjectSearcher                             ( SELECT   *   FROM   Win_Processor );

  string   tmpUint_   =   stringEmpty;

  foreach(ManagementObject   WmiObj   in   WmiGet())

  {

  tmpUint_   =WmiObj[ ProcessorId ]ToString();

  }

  thistextBoxText   =   tmpUint_;

  thisgProgressBarValue   ++;

  }

  catch(Exception   ex)

  {

  throw   new   Exception(exToString());

  }

  }

  //取第一塊硬盤編號

  public string getharddiskid()

  {

  try

  {

  managementobjectsearcher searcher = new managementobjectsearcher(select * from win_physicalmedia);

  string strharddiskid = null ;

  foreach(managementobject mo in searcherget())

  {

  strharddiskid = mo[serialnumber]tostring()trim();

  break;

  }

  return strharddiskid ;

  }

  catch

  {

  return ;

  }

  }//end

  //獲取磁盤序列號

  public string getharddiskid()

  {

  try

  {

  SystemManagementManagementObjectSearcher   cmicWmi   =   new                                      ManagementObjectSearcher( SELECT   *   FROM   Win_DiskDrive );

  SystemUInt   tmpUint   =   ;

  foreach(ManagementObject   cmicWmiObj   in   cmicWmiGet())

  {

  tmpUint   =   ConvertToUInt(cmicWmiObj[ signature ]ToString());

  }

  thistextBoxText   =   tmpUintToString();

  thisgProgressBarValue   ++;

  }

  catch(Exception   ex)

  {

  throw   new   Exception(exToString());

  }

  }

  ///   <summary>

  ///   獲取硬盤ID

  ///   </summary>

  ///   <returns> string </returns>

  public string GetHDid()

  {

  string HDid = ;

  using (ManagementClass cimobject = new ManagementClass(Win_DiskDrive))

  {

  ManagementObjectCollection moc = cimobjectGetInstances();

  foreach (ManagementObject mo in moc)

  {

  HDid = (string)moProperties[Model]Value;

  moDispose();

  }

  }

  return HDidToString();

  }

  ///   <summary>

  ///   獲取網卡硬件地址

  ///   </summary>

  ///   <returns> string </returns>

  public string GetMoAddress()

  {

  string MoAddress = ;

  using (ManagementClass mc = new ManagementClass(Win_NetworkAdapterConfiguration))

  {

  ManagementObjectCollection moc = mcGetInstances();

  foreach (ManagementObject mo in moc)

  {

  if ((bool)mo[IPEnabled] == true)

  MoAddress = mo[MacAddress]ToString();

  moDispose();

  }

  }

  return MoAddressToString();

  }

  }

  }


From:http://tw.wingwit.com/Article/program/net/201311/13763.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.