Get System Information Using C# Code

Introduction

It's very common to want a trial version of your application. 3 days ago I created a software for a hotel and restaurant. In that software I created a trial version. For that purpose I used all the system information and saved that information.
 
This code shows how to retrieve much information about the system, like computer ID, hard disk, processors, operating system, other hardware and so on. For this we will use the System.Management namespace.
 
Here I am creating a simple Windows application to get the system information. 
Procedure
  1. Open Visual Studio and create a new project for a Windows Forms application. 

  2. Now add a form to the project. 

  3. On that form create one ComboBox and one Button and one DataGridView like the following:



  4. Now bind the following items list with the ComboBox:

    a. Win32_ComputerSystem

    b. Win32_DiskDrive

    c. Win32_OperatingSystem

    d. Win32_Processor

    e. Win32_ProgramGroup

    f. Win32_SystemDevices

    g. Win32_StartupCommand

  5. Now on the button click event write the following code:
    1. dgvWMI.DataSource = GetInformation(comboBoxWin32API.Text);  
  6. Now add the System.Management library to your project.

  7. Now here is my actual code to get the information about the system.
    1. private ArrayList GetInformation(string qry)    
    2. {    
    3.     ManagementObjectSearcher searcher;    
    4.     int i = 0;    
    5.     ArrayList arrayListInformationCollactor = new ArrayList();    
    6.     try    
    7.     {    
    8.         searcher = new ManagementObjectSearcher("SELECT * FROM " + qry);    
    9.         foreach (ManagementObject mo in searcher.Get())    
    10.         {    
    11.             i++;    
    12.             PropertyDataCollection searcherProperties = mo.Properties;    
    13.             foreach (PropertyData sp in searcherProperties)    
    14.             {    
    15.                 arrayListInformationCollactor.Add(sp);    
    16.             }    
    17.         }    
    18.     }    
    19.     catch (Exception ex)    
    20.     {    
    21.         MessageBox.Show(ex.ToString());    
    22.     }    
    23.     return arrayListInformationCollactor;    
    24. }  
  8. Output: After running the project you will get the following output.
table
 
Happy Coding :) 

Up Next
    Ebook Download
    View all
    Learn
    View all
    sourabhsomani.com