C # obtain computer-related information

Source: Internet
Author: User
Tags servervariables

/* Creator: the blog of caidao jushi
* Creation date: January 1, August 31, 2014
* Function: obtains information about a computer.
*
*/

Namespace net. String. consoleapplication
{
Using system;
Using system. Management;

Public class computerhelper
{
/// <Summary>
/// Obtain the CPU serial number code
/// </Summary>
Public static string getcpuid ()
{
Return execinfo () => {
Managementclass MC = new managementclass ("win32_processor ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
Return Mo. properties ["processorid"]. value. tostring ();
}
Return string. empty;
});
}

/// <Summary>
/// Obtain the MAC address of the NIC
/// </Summary>
Public static string getmacaddress ()
{
Return execinfo () => {
Managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
If (bool) Mo ["ipenabled"] = true)
{
Return Mo ["macaddress"]. tostring ();
}
}
Return string. empty;
});
}

/// <Summary>
/// Obtain the hard disk ID
/// </Summary>
Public static string getdiskid ()
{
Return execinfo () => {
Managementclass MC = new managementclass ("win32_diskdrive ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
Return (string) Mo. properties ["model"]. value;
}
Return string. empty;
});
}

/// <Summary>
/// Username used to log on to the Operating System
/// </Summary>
Public static string GetUserName ()
{
Return execinfo () => {
Managementclass MC = new managementclass ("win32_computersystem ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
Return Mo ["username"]. tostring ();
}
Return string. empty;
});
}

/// <Summary>
/// Pc type
/// </Summary>
Public static string getsystemtype ()
{
Return execinfo () =>
{
Managementclass MC = new managementclass ("win32_computersystem ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
Return Mo ["systemtype"]. tostring ();
}
Return string. empty;
});
}

/// <Summary>
/// Physical memory
/// </Summary>
Public static string gettotalphysicalmemory ()
{
Return execinfo () =>
{
Managementclass MC = new managementclass ("win32_computersystem ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
Return Mo ["totalphysicalmemory"]. tostring ();
}
Return string. empty;
});
}

/// <Summary>
/// Computer name
/// </Summary>
Public static string getcomputername ()
{
Return execinfo () => {
Return System. environment. getenvironmentvariable ("computername ");
});
}

Private Static string execinfo (func <string> func)
{
String result = string. empty;
Try
{
Result = func ();
}
Catch
{
Return "unknow ";
}
Finally {}
Return result;
}

/// <Summary>
/// Obtain the Client IP Address
/// </Summary>
Public String getclientidaddress ()
{
String IP = string. empty;
Try
{
If (system. Web. httpcontext. Current. Request. servervariables ["http_x_forwarded_for"] = NULL)
IP = system. Web. httpcontext. Current. Request. servervariables ["remote_addr"]. tostring ();
Else
IP = system. Web. httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]. tostring ();

If (string. isnullorempty (IP ))
{
IP = system. Web. httpcontext. Current. Request. userhostaddress;
}
}
Catch {IP = "1.1.1.1 ";}
Return IP;
}


}
}

C # obtain computer-related information

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.