日期:2011-07-18  浏览次数:20518 次

建立一个类.然后读取信息.调用代码如下.
HardInfoClass myclass=new HardInfoClass();
   textBox1.Text=myclass.GetHardDiskID();
   textBox2.Text=myclass.GetCpuID();
   textBox3.Text=myclass.GetNetCardMAC();
   textBox4.Text=myclass.GetNetCardIP();
   textBox5.Text=myclass.GetHostName();
   textBox6.Text=myclass.GetVolOf("D");//C盘58c6b679跟D盘6ed62864不一样

   //textBox7.Text=myclass.GetHashCode();
   //textBox8.Text=myclass.GetCpuID();  

类HardInfoClass代码如下

using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Management; //需要在解决方案中引用System.Management.DLL文件

namespace FileTransLate.PCStatus
{
 /// <summary>
 /// HardInfoClass 的摘要说明。
 /// </summary>
 public class HardInfoClass
 {
  

  [DllImport("kernel32.dll")]
  private static extern int GetVolumeInformation(
   string lpRootPathName,
   string lpVolumeNameBuffer,
   int nVolumeNameSize,
   ref int lpVolumeSerialNumber,
   int lpMaximumComponentLength,
   int lpFileSystemFlags,
   string lpFileSystemNameBuffer,
   int nFileSystemNameSize
   );

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

  //取机器名
  public string GetHostName()
  {
   return System.Net.Dns.GetHostName();
  }

  //取CPU编号
  public string GetCpuID()
  {
   try
   {
    ManagementClass mc = new ManagementClass("Win32_Processor");
    ManagementObjectCollection moc = mc.GetInstances();

    string strCpuID = null ;
    foreach( ManagementObject mo in moc )
    {
     strCpuID = mo.Properties["ProcessorId"].Value.ToString();
     break;
    }
    return strCpuID;
   }
   catch
   {
    return "";
   }

  }//end method

  //取第一块硬盘编号
  public string GetHardDiskID()
  {
   try
   {
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
    string strHardDiskID = null ;
    foreach(ManagementObject mo in searcher.Get())
    {
     strHardDiskID = mo["SerialNumber"].ToString().Trim();
     break;
    }
    return strHardDiskID ;
   }
   catch
   {
    return "";
   }
  }
  //获取网卡MAC地址

  public string GetNetCardMAC()
  {
   try
   {
    string stringMAC = "";    
    ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection MOC= MC.GetInstances();
   
    foreach(Manag