Note: first, add reference system. Management in the project bin directory.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Management;
/// <Summary>
/// Computer Summary
/// </Summary>
Public class computer
{
Public String cpuid; // CPU ID
Public int cpucount; // Number of CPUs
Public String [] cpumhz; // CPU frequency unit: Hz
Public String macaddress; // the MAC address of the computer
Public String diskid; // hard disk ID
Public String disksize; // hard disk size unit: bytes
Public String IPaddress; // the IP address of the computer.
Public String loginusername; // operating system logon Username
Public String computername; // computer name
Public String systemtype; // system type
Public String totalphysicalmemory; // total memory unit: m
Private Static computer _ instance;
Public static computer instance ()
{
If (_ instance = NULL)
_ Instance = new computer ();
Return _ instance;
}
Public Computer ()
{
Cpuid = getcpuid ();
Cpucount = getcpucount ();
Cpumhz = getcpumhz ();
Macaddress = getmacaddress ();
Diskid = getdiskid ();
Disksize = getsizeofdisk ();
IPaddress = getipaddress ();
Loginusername = GetUserName ();
Systemtype = getsystemtype ();
Totalphysicalmemory = gettotalphysicalmemory ();
Computername = getcomputername ();
}
String getcpuid ()
{
Try
{
// Obtain the CPU serial number Code
String cpuinfo = ""; // CPU serial number
Managementclass MC = new managementclass ("win32_processor ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
Cpuinfo = Mo. properties ["processorid"]. value. tostring ();
}
MoC = NULL;
MC = NULL;
Return cpuinfo;
}
Catch
{
Return "unknow ";
}
Finally
{
}
}
Public static int getcpucount ()
{
Try
{
Using (managementclass mcpu = new managementclass ("win32_processor "))
{
Managementobjectcollection CPUs = mcpu. getinstances ();
Return CPUs. count;
}
}
Catch
{
}
Return-1;
}
Public static string [] getcpumhz ()
{
Managementclass MC = new managementclass ("win32_processor ");
Managementobjectcollection CPUs = mc. getinstances ();
String [] MHz = new string [CPUs. Count];
Int C = 0;
Managementobjectsearcher MySearch = new managementobjectsearcher ("select * From win32_processor ");
Foreach (managementobject Mo in MySearch. Get ())
{
MHz [c] = Mo. properties ["currentclockspeed"]. value. tostring ();
C ++;
}
MC. Dispose ();
MySearch. Dispose ();
Return MHz;
}
Public static string getsizeofdisk ()
{
Managementclass MC = new managementclass ("win32_diskdrive ");
Managementobjectcollection MOJ = mc. getinstances ();
Foreach (managementobject m in MOJ)
{
Return M. properties ["size"]. value. tostring ();
}
Return "-1 ";
}
String getmacaddress ()
{
Try
{
// Obtain the hardware address of the NIC
String MAC = "";
Managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
If (bool) Mo ["ipenabled"] = true)
{
MAC = Mo ["macaddress"]. tostring ();
Break;
}
}
MoC = NULL;
MC = NULL;
Return MAC;
}
Catch
{
Return "unknow ";
}
Finally
{
}
}
String getipaddress ()
{
Try
{
// Obtain the IP address
String ST = "";
Managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
If (bool) Mo ["ipenabled"] = true)
{
// St = Mo ["IPaddress"]. tostring ();
System. array Ar;
AR = (system. array) (Mo. properties ["IPaddress"]. value );
St = ar. getvalue (0). tostring ();
Break;
}
}
MoC = NULL;
MC = NULL;
Return st;
}
Catch
{
Return "unknow ";
}
Finally
{
}
}
String getdiskid ()
{
Try
{
// Obtain the hard disk ID
String Hdid = "";
Managementclass MC = new managementclass ("win32_diskdrive ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
Hdid = (string) Mo. properties ["model"]. value;
}
MoC = NULL;
MC = NULL;
Return Hdid;
}
Catch
{
Return "unknow ";
}
Finally
{
}
}< BR >///
// username for logging on to the Operating System
///
///
string GetUserName ()
{< br> try
{< br> string ST = "";
managementclass MC = new managementclass ("win32_computersystem ");
managementobjectcollection MOC = MC. getinstances ();
foreach (managementobject Mo in MoC)
{
St = Mo ["username"]. tostring ();
}
MoC = NULL;
MC = NULL;
Return st;
}
Catch
{
Return "unknow ";
}
Finally
{
}
}
String getsystemtype ()
{
Try
{
String ST = "";
Managementclass MC = new managementclass ("win32_computersystem ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
St = Mo ["systemtype"]. tostring ();
}
MoC = NULL;
MC = NULL;
Return st;
}
Catch
{
Return "unknow ";
}
Finally
{
}
}
String gettotalphysicalmemory ()
{
Try
{
String ST = "";
Managementclass MC = new managementclass ("win32_computersystem ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
St = Mo ["totalphysicalmemory"]. tostring ();
}
MoC = NULL;
MC = NULL;
Return st;
}
Catch
{
Return "unknow ";
}
Finally
{
}
}
String getcomputername ()
{
Try
{
Return System. environment. getenvironmentvariable ("computername ");
}
Catch
{
Return "unknow ";
}
Finally
{
}
}
}