Click here to Skip to main content
11,581,982 members (78,090 online)
Click here to Skip to main content

A simple application for gathering system information

, 8 Apr 2012 CPOL 9K 900 20
Rate this:
Please Sign up or sign in to vote.
In this tip you will learn how to collect system information by using System.Environment and System.Management.

System Information

CPU Information

Local Drives Information

Introduction

In this application, user system information has been collected by using two classes:

  1. System.Environment
  2. System.Managememnt

Using the Code

The main tab in this application has three items:

  1. System
  2. CPU
  3. Local drives

In the System tab, the System.Environment class has been used to gather information:

private string SystemInformation()
{
    StringBuilder StringBuilder1 = new StringBuilder(string.Empty);
    try
    {
        StringBuilder1.AppendFormat("Operation System:  {0}\n", Environment.OSVersion);
        if (Environment.Is64BitOperatingSystem)
            StringBuilder1.AppendFormat("\t\t  64 Bit Operating System\n");
        else
            StringBuilder1.AppendFormat("\t\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\t VolumeLabel: " + 
                  "{1}\n\t\t DriveType: {2}\n\t\t DriveFormat: {3}\n\t\t " + 
                  "TotalSize: {4}\n\t\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();
}

In the CPU and Local drive tabs, the System.Management class has been used to gather information but before using this class, it has to be added to references. Add System.Management to references:

  1. Go to Solution Explorer.
  2. Right click on References and select Add Reference.
  3. Click on .NET tab.
  4. Select System.Management.
  5. Select the OK button.

Now we can use this class in the following function:

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();
}

Notice that this function has an input string which can be Win32_Processor or Win32_LogicalDisk. We use the first one for collecting information about CPU and the second one for local drives. Now in the Window_Loaded event, we call these functions:

//System Information
textBox1.Text = SystemInformation();
//CPU Information
textBox2.Text = DeviceInformation("Win32_Processor");
//Local Drives Information
textBox3.Text = DeviceInformation("Win32_LogicalDisk");

Points of Interest

In this tip, you learned how to collect system information by using System.environment and System.managememnt.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

MehdiNaseri
Chief Technology Officer National Iranian Oil Company (NIOC)
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
- Timeline of my career focus:
1994: Batch files in Microsoft Dos
1995: GW-Basic, Q Basic
1996: Assembly, C
1999: Pascal, C++
2000: Bachelor of Science Degree in Computer Software Engineering
2001: Borland Delphi, Microsoft SQL Server, HTML
2002: Visual Basic, C--, Ada, Lisp, Prolog, JavaScript
2003: C#, Java
2004: Asp.net, Computer Networks
2005: Master of Science Degree in Computer Software Engineering
2006: Data Mining
2007: XML, XQuery, XSLT
2008: WPF
2009: Silverlight, Network Administration
2010: ASP.NET MVC, JQuery, CSS, Ajax, LINQ, Entity Framework
2011: VMware Visualization (vSphere, vCenter)
2012: Microsoft SharePoint, Microsoft System Center, Microsoft Visualization (Hyper-V), Microsoft Exchange Server, Microsoft Lync, Windows 8 App
2013: Cloud Computing (VMware and Microsoft Hyper-V),
2014: ???

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
shelby6712-May-12 9:11
membershelby6712-May-12 9:11 
GeneralRe: My vote of 4 Pin
MehdiNaseri27-Jan-13 23:02
memberMehdiNaseri27-Jan-13 23:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 8 Apr 2012
Article Copyright 2012 by MehdiNaseri
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid