c#擷取本機電腦相關資訊

來源:互聯網
上載者:User

建立一個類.然後讀取資訊.調用代碼如下.
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(ManagementObject MO in MOC)
    {
     if ((bool)MO["IPEnabled"] == true)
     {
      stringMAC += MO["MACAddress"].ToString();
            
     }
    } 
    return stringMAC;
   }
   catch
   {
    return "";
   }
  }

  //擷取硬碟資訊的代碼
  public string GetVolOf(string drvID)
  {
   try
   {
   const int MAX_FILENAME_LEN = 256;
   int retVal = 0;
   int a =0;
   int b =0;
   string str1 = null;
   string str2 = null;


   int i = GetVolumeInformation(
    drvID + @":\",
    str1,
    MAX_FILENAME_LEN,
    ref retVal,
    a,
    b,
    str2,
    MAX_FILENAME_LEN
    );

   return retVal.ToString("x");
   }
   catch
   {
    return "";
   }
  }


  //擷取當前網卡IP地址
  public string GetNetCardIP()
  {
   try
   {   
    string stringIP = "";
    ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection MOC= MC.GetInstances();
   
    foreach(ManagementObject MO in MOC)
    {
     if ((bool)MO["IPEnabled"] == true)
     {      
      string[] IPAddresses = (string[]) MO["IPAddress"];
      if(IPAddresses.Length > 0)
      stringIP = IPAddresses[0].ToString();
      
     }
    }
    return stringIP;
   }
   catch
   {
    return "";
   }
  }
  
 }
}



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。