程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> c#獲取本機電腦相關信息

c#獲取本機電腦相關信息

編輯:關於C語言

建立一個類.然後讀取信息.調用代碼如下.
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 "";
   }
  }
  
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved