C # program for Obtaining user system information

Source: Internet
Author: User

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 ");

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.