C # obtain information about the Local Computer

Source: Internet
Author: User

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

}
}
 

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.