/* 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