Create a computer hardware information class and read the information. The call code is as follows.
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"); // drive C 58c6b679 is different from drive D 6ed62864
// Textbox7.text = myclass. gethashcode ();
// Textbox8.text = myclass. getcpuid ();
The code for the class hardinfoclass is as follows:
Using system;
Using system. net;
Using system. runtime. interopservices;
Using system. Management; // You must reference the system. Management. dll file in the solution.
Namespace filetranslate. pcstatus
{
/// <Summary>
/// Summary of 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: add the constructor logic here
//
}
// Obtain the machine name
Public String gethostname ()
{
Return System. net. DNS. gethostname ();
}
// Obtain the CPU ID
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
// Obtain the first hard disk number
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 "";
}
}
// Obtain the MAC address of the NIC
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 "";
}
}
// Code for obtaining hard disk Information
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 "";
}
}
// Obtain the IP address of the current Nic
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 "";
}
}
}
}
Source: feino net (http://dev.firnow.com/course/4_webprogram/asp.net/asp_netshl/2008422/111131_2.html)