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

Step 1. Open Visual Studio and create a new project for a Windows Forms application.

Step 2. Now add a form to the project.

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

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

  • Win32_ComputerSystem
  • Win32_DiskDrive
  • Win32_OperatingSystem
  • Win32_Processor
  • Win32_ProgramGroup
  • Win32_SystemDevices
  • Win32_StartupCommand

Step 5. Now on the button click event write the following code

dgvWMI.DataSource = GetInformation(comboBoxWin32API.Text);

Step 6. Now add the System.Management library to your project.

Now here is my actual code to get the information about the system.

private ArrayList GetInformation(string qry)
{
    ManagementObjectSearcher searcher;
    int i = 0;
    ArrayList arrayListInformationCollator = new ArrayList();

    try
    {
        searcher = new ManagementObjectSearcher("SELECT * FROM " + qry);
        foreach (ManagementObject mo in searcher.Get())
        {
            i++;
            PropertyDataCollection searcherProperties = mo.Properties;
            foreach (PropertyData sp in searcherProperties)
            {
                arrayListInformationCollator.Add(sp);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }

    return arrayListInformationCollator;
}

Output

After running the project you will get the following output.

table


Similar Articles