C # general settings of Computer Detection

Source: Internet
Author: User

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
{
}
}

}
}

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.