Using System;
Using System. Management;
Namespace Soyee. Comm
{
/// <Summary>
/// Computer Information
/// </Summary>
Public class Computer
{
Public string CpuID;
Public string MacAddress;
Public string DiskID;
Public string IpAddress;
Public string LoginUserName;
Public string ComputerName;
Public string SystemType;
Public string TotalPhysicalMemory; // unit: M
Private static Computer _ instance;
Public static Computer Instance ()
{
If (_ instance = null)
_ Instance = new Computer ();
Return _ instance;
}
Protected Computer ()
{
CpuID = GetCpuID ();
MacAddress = GetMacAddress ();
DiskID = GetDiskID ();
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
{
}
}
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
{
}
}
/// <Summary>
/// Username used to log on to the Operating System
/// </Summary>
/// <Returns> </returns>
String GetUserName ()
{
Try
{
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
{
}
}
/// <Summary>
/// PC type
/// </Summary>
/// <Returns> </returns>
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
{
}
}
/// <Summary>
/// Physical memory
/// </Summary>
/// <Returns> </returns>
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
{
}
}
/// <Summary>
///
/// </Summary>
/// <Returns> </returns>
String GetComputerName ()
{
Try
{
Return System. Environment. GetEnvironmentVariable ("ComputerName ");
}
Catch
{
Return "unknow ";
}
Finally
{
}
}
}
}