C # To obtain information about the local computer

Source: Internet
Author: User
Tags foreach bool tostring

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

}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.