In this program, the user system information is obtained through the following two classes:
System. Environment
System. Managememnt
The application includes three tabs:
System
CPU
Local drives
In the System menu, use the System. Environment class to obtain System information:
[Csharp]
Private string SystemInformation ()
{
StringBuilder StringBuilder1 = new StringBuilder (string. Empty );
Try
{
StringBuilder1.AppendFormat ("Operation System: {0} \ n", Environment. OSVersion );
If (Environment. Is64BitOperatingSystem)
StringBuilder1.AppendFormat ("\ t 64 Bit Operating System \ n ");
Else
StringBuilder1.AppendFormat ("\ t 32 Bit Operating System \ n ");
StringBuilder1.AppendFormat ("SystemDirectory: {0} \ n", Environment. SystemDirectory );
StringBuilder1.AppendFormat ("ProcessorCount: {0} \ n", Environment. ProcessorCount );
StringBuilder1.AppendFormat ("UserDomainName: {0} \ n", Environment. UserDomainName );
StringBuilder1.AppendFormat ("UserName: {0} \ n", Environment. UserName );
// Drives
StringBuilder1.AppendFormat ("LogicalDrives: \ n ");
Foreach (System. IO. DriveInfo DriveInfo1 in System. IO. DriveInfo. GetDrives ())
{
Try
{
StringBuilder1.AppendFormat ("\ t Drive: {0} \ n \ t VolumeLabel:" +
"{1} \ n \ t DriveType: {2} \ n \ t DriveFormat: {3} \ n \ t" +
"TotalSize: {4} \ n \ t AvailableFreeSpace: {5} \ n ",
DriveInfo1.Name, DriveInfo1.VolumeLabel, DriveInfo1.DriveType,
DriveInfo1.DriveFormat, DriveInfo1.TotalSize, DriveInfo1.AvailableFreeSpace );
}
Catch
{
}
}
StringBuilder1.AppendFormat ("SystemPageSize: {0} \ n", Environment. SystemPageSize );
StringBuilder1.AppendFormat ("Version: {0}", Environment. Version );
}
Catch
{
}
Return StringBuilder1.ToString ();
}
On the CPU and Local drive tabs, class System. Management is used to collect information, but before using this class, you need to add reference. Add System. Management reference as follows:
Open the project solution.
Right-click reference and choose add reference.
Click the. NET tab.
Select System. Management.
Click OK.
Now you can use this class to write down the following functions:
[Csharp]
Private string DeviceInformation (string stringIn)
{
StringBuilder StringBuilder1 = new StringBuilder (string. Empty );
ManagementClass ManagementClass1 = new ManagementClass (stringIn );
// Create a ManagementObjectCollection to loop through
ManagementObjectCollection ManagemenobjCol = ManagementClass1.GetInstances ();
// Get the properties in the class
PropertyDataCollection properties = ManagementClass1.Properties;
Foreach (ManagementObject obj in ManagemenobjCol)
{
Foreach (PropertyData property in properties)
{
Try
{
StringBuilder1.AppendLine (property. Name + ":" +
Obj. Properties [property. Name]. Value. ToString ());
}
Catch
{
// Add codes to manage more informations
}
}
StringBuilder1.AppendLine ();
}
Return StringBuilder1.ToString ();
}
This function has a parameter whose value can be Win32_Processor or Win32_LogicalDisk. The first parameter is used to obtain the CPU information, and the second parameter is used to obtain the hard drive information. Finally, the Window_Loaded function calls the following statement:
[Csharp]
// System Information
TextBox1.Text = SystemInformation ();
// CPU Information
TextBox2.Text = DeviceInformation ("Win32_Processor ");
// Local Drives Information
TextBox3.Text = DeviceInformation ("Win32_LogicalDisk ");