Create a class. And then read the information. The calling 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");//c plate 58c6b679 is different from D disk 6ed62864
Textbox7.text=myclass. GetHashCode ();
Textbox8.text=myclass. Getcpuid ();
The class Hardinfoclass code is as follows
Using System;
Using System.Net;
Using System.Runtime.InteropServices;
Using System.Management; Need to refer to the System.Management.DLL file in the solution
Namespace Filetranslate.pcstatus
{
<summary>
Summary description of the 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 constructor logic here
//
}
Take the machine name
public string GetHostName ()
{
return System.Net.Dns.GetHostName ();
}
Take CPU number
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
Take the first hard drive 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 "";
}
}
Get the NIC MAC address
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 to get the hard drive 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 "";
}
}
Get current network card IP address
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 "";
}
}
}
}