C#获取当前计算机的硬件及系统信息

C#获取当前计算机的系统信息

//系统标识符和版本号
   string strSystem = Environment.OSVersion.ToString();
   //获取映射到进程上下文的物理内存量
   string strRem=Environment.WorkingSet.ToString();
   //获取系统启动后经过的毫秒数
   int iTC=Environment.TickCount/60000;
   //系统目录的完全限定路径
   string strSD=Environment.SystemDirectory;
   //获取此本地计算机的 NetBIOS 名称
   string strMN=Environment.MachineName;
   //获取与当前用户关联的网络域名
   string strUDN=Environment.UserDomainName;


-------------------------------------------------------------------------------------
   //输出所有驱动器号
   string[] drv=System.IO.Directory.GetLogicalDrives();
还有文件信息,最后修改时间等等

获取某一目录下的所有文件信息
   //当前目录下的所有文件夹
   DirectoryInfo di=new DirectoryInfo(strPath);
   DirectoryInfo[] wjj=di.GetDirectories();
//此代码从网络收集整理
//经测试通过
//需引用System.Management

using System;
using System.Management;

namespace MyCustomClassLib
{
 ///


 /// 硬件信息类
 ///

 public class HardwareInfo
 {
  public HardwareInfo()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }

  #region 硬件属性
  ///


  /// 机器名
  ///

  public string HostName
  {
   get{return System.Net.Dns.GetHostName();}   
  }

  ///


  /// CPU编号
  ///

  public string CPUID
  {
   get{return GetCpuID();}
  }

  ///


  /// 硬盘编号
  ///

  public string HardDiskID
  {
   get{return GetHardDiskID();}
  }

  ///


  /// 网卡MAC
  ///

  public string NetMac
  {
   get{return GetMac();}
  }
  
  #endregion

  #region 获取硬件信息的方法
  ///


  /// 获得CPU编号
  ///

  ///
  private string GetCpuID()
  {
   string result="";
   try
   {
    ManagementClass mc = new ManagementClass("Win32_Processor");
    ManagementObjectCollection moc = mc.GetInstances();     
    
    foreach( ManagementObject mo in moc )
    {
     result=mo.Properties["ProcessorId"].Value.ToString();     
    }    
   }
   catch
   {
    return "获取CPUID失败";
   }
   return result;
  }

  ///


  /// 获得硬盘编号
  ///

  ///
  private string GetHardDiskID()
  {
   string result="";
   try
   {
    ManagementClass mcHD=new ManagementClass("win32_logicaldisk");
    ManagementObjectCollection mocHD=mcHD.GetInstances();
    foreach(ManagementObject m in mocHD)
    {
     if(m["DeviceID"].ToString()=="C:")
     {
      result=m["VolumeSerialNumber"].ToString().Trim();      
     }
    }
   }
   catch
   {
    return "获取硬盘ID失败";
   }
   return result;
  }

  ///


  /// 获得网卡MAC
  ///

  ///
  private string GetMac()
  {
   string result="";
   try
   {
    ManagementClass mcMAC=new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection mocMAC=mcMAC.GetInstances();
    foreach(ManagementObject m in mocMAC)
    {
     if((bool)m["IPEnabled"])
     {
      result=m["MacAddress"].ToString();     
     }
    }
   }
   catch
   {
    return "获取MAC失败";
   } 
   return result;
  }

  #endregion
 }

 

 

 

获取CPU编号:
view plaincopy to clipboardprint?
ManagementClass mc = new ManagementClass("Win32_Processor"); 
ManagementObjectCollection moc = mc.GetInstances(); 
string strID = null ; 
foreach( ManagementObject mo in moc ) 

strID = mo.Properties["ProcessorId"].Value.ToString(); 
break; 

textBox1.Text +=  "CPU ID:" + strID; 
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
string strID = null ; 
foreach( ManagementObject mo in moc ) 

strID = mo.Properties["ProcessorId"].Value.ToString();
break;

textBox1.Text +=  "CPU ID:" + strID;

电脑1:CPU ID:BFEBFBFF00000F27

电脑2:CPU ID:BFEBFBFF00000F27

电脑3:CPU ID:BFEBFBFF00000F29

电脑4:CPU ID:BFEBFBFF00000F29

获取主板编号:
view plaincopy to clipboardprint?
ManagementClass mc = new ManagementClass("Win32_BaseBoard"); 
ManagementObjectCollection moc = mc.GetInstances(); 
string strID = null ; 
foreach( ManagementObject mo in moc ) 

strID = mo.Properties["SerialNumber"].Value.ToString(); 
break; 

textBox1.Text +=  "主板 ID:" + strID; 
ManagementClass mc = new ManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc = mc.GetInstances();
string strID = null ; 
foreach( ManagementObject mo in moc ) 

strID = mo.Properties["SerialNumber"].Value.ToString();
break;

textBox1.Text +=  "主板 ID:" + strID;

电脑2:主板 ID:CN24401483

电脑3:主板 ID:AZF241001101

获取硬盘编号:
view plaincopy to clipboardprint?
ManagementClass mc = new ManagementClass("Win32_PhysicalMedia"); 
//网上有提到,用Win32_DiskDrive,但是用Win32_DiskDrive获得的硬盘信息中并不包含SerialNumber属性。 
ManagementObjectCollection moc = mc.GetInstances(); 
string strID = null ; 
foreach( ManagementObject mo in moc ) 

strID = mo.Properties["SerialNumber"].Value.ToString(); 
break; 

textBox1.Text +=  "硬盘 ID:" + strID; 
ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");
//网上有提到,用Win32_DiskDrive,但是用Win32_DiskDrive获得的硬盘信息中并不包含SerialNumber属性。
ManagementObjectCollection moc = mc.GetInstances();
string strID = null ; 
foreach( ManagementObject mo in moc ) 

strID = mo.Properties["SerialNumber"].Value.ToString();
break;

textBox1.Text +=  "硬盘 ID:" + strID;

电脑1:硬盘 ID:4833395344463658202020202020202020202020

电脑2:硬盘 ID:WD-WMAJD1092385

电脑3:硬盘 ID:4a353756354d5939202020202020202020202020

电脑4:硬盘 ID:0637J2FW508014


获取BIOS编号:
view plaincopy to clipboardprint?
ManagementClass mc = new ManagementClass("Win32_BIOS"); 
ManagementObjectCollection moc = mc.GetInstances(); 
string strID = null ; 
foreach( ManagementObject mo in moc ) 

strID = mo.Properties["SerialNumber"].Value.ToString(); 
break; 

textBox1.Text +=  "BIOS ID:" + strID; 
ManagementClass mc = new ManagementClass("Win32_BIOS");
ManagementObjectCollection moc = mc.GetInstances();
string strID = null ; 
foreach( ManagementObject mo in moc ) 

strID = mo.Properties["SerialNumber"].Value.ToString();
break;

textBox1.Text +=  "BIOS ID:" + strID;

电脑2:BIOS ID:CN24401483

由以上各步看出,通过Win32_Processor获取CPUID不正确,或者说Win32_Processor字段就不包含CPU编号信息。

通过Win32_BaseBoard获取主板信息,但不是所有的主板都有编号,或者说不是能获取所有系统主板的编号。

通过Win32_PhysicalMedia获取硬盘编号应该没有问题。但网上说可以通过Win32_DiskDrive获取,其实所得信息根本不包含SerialNumber。

通过Win32_BIOS获取BIOS信息,基本和获取主板信息差不多。就是说:不是所有的主板BIOS信息都有编号。


另外,可以将通过以上各字段所得信息输出,逐个查看所有信息 属性和对应的值。代码如下:

view plaincopy to clipboardprint?
ManagementClass mc = new ManagementClass("Win32_Processor"); 
ManagementObjectCollection moc = mc.GetInstances(); 
foreach( ManagementObject mo in moc ) 

textBox1.Text += "/r/n============CUP信息==========="; 
foreach (PropertyData pd in mo.Properties) 

textBox1.Text += "/r/n" + pd.Name + "/t"; 
if (pd.Value != null) 

textBox1.Text += pd.Value.ToString(); 


textBox1.Text += "/r/n/r/n======================="; 


你可能感兴趣的:(C#示例)