C 獲取硬體資訊

2021-09-05 21:53:16 字數 2996 閱讀 9001

using system;

using system.net;

using system.runtime.interopservices;

using system.management; //需要在解決方案中引用system.management.dll檔案

///

/// hardinfoclass 的摘要說明。

///

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()

trymanagementclass 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()

trymanagementobjectsearcher 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()

trystring 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)

tryconst 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()

trystring 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 "";

}今天以外的需要獲得mac,沒想到找到了這個,還是蠻有意思的!

c 獲取硬體資訊

using system using system.runtime.interopservices using system.management namespace hardware 取cpu編號 public string getcpuid return strcpuid catch end m...

c 獲取硬體資訊

using system using system.runtime.interopservices using system.management namespace hardware 取cpu編號 public string getcpuid return strcpuid catch end m...

C 獲取硬體資訊

using system using system.net using system.runtime.interopservices using system.management 需要在解決方案中引用system.management.dll檔案 獲取機器名 public string getho...