Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Get System Info using C#

0.00/5 (No votes)
9 Sep 2004 6  
An Article that shows how to use System.Management in C# to get System Information

Introduction

This code sample shows how System.Management namespace can be used to retrieve information about the system. It can be used to get a lot of information about the system like Hard Disk, Processors, Operating system, other hardware etc.

Using the code

Please download the entire source code. This sample is a windows application. Here are the steps you can take to create your own application.

  • Start with a new C# Windows application.
  • Add a form to the project. Add two buttons (Show and Cancel) on the form. Add a datagrid on the form. Add one Combo box and two labels on the form.
  • Go to references and add System.Management from the .NET tab.
  • Now make sure that you have the following in your code
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;

You are now ready to add the actual code that lets you work with the Win32. We will create a new function GetStuff

public
ArrayList GetStuff(string
           queryObject)
{
  ManagementObjectSearcher
    searcher;
  int
    i = 0;
  ArrayList
    hd = new
    ArrayList();
  try
  {
    searcher
      = new
      ManagementObjectSearcher(
        "SELECT * FROM " + queryObject);
    foreach(ManagementObject
      wmi_HD in
      searcher.Get())
    {
      i++;
      PropertyDataCollection
        searcherProperties = wmi_HD.Properties;
      foreach
        (PropertyData sp in
        searcherProperties) 

      {
        hd.Add(sp);
      }
    }
  }
  catch(Exception
    ex)
  {
    MessageBox.Show(ex.ToString());
  }
  return
    hd;
}

Now we are ready to use this. For that, you would need to call this function from the button click. Then bind it to the datagrid and you are good to go.

Here is the entire source code...

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;

namespace SystemInfo
{
  /// <summary>

  /// Summary description for Form1.

  /// </summary>

  public class Form1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.ComboBox comboBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.DataGrid datagrid1;
    /// <summary>

    /// Required designer variable.

    /// </summary>

    private System.ComponentModel.Container components = null;

    public Form1()
    {
      //

      // Required for Windows Form Designer support

      //

      InitializeComponent();

      //

      // TODO: Add any constructor code after 

      // InitializeComponent call

      //

    }
    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]
    static void Main() 
    {
      Application.Run(new Form1());
    }
    /// <summary>

    /// Clean up any resources being used.

    /// </summary>

    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

  #region Windows Form Designer generated code
    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InitializeComponent()
    {
      System.Resources.ResourceManager resources = 
        new System.Resources.ResourceManager(typeof(Form1));
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.button2 = new System.Windows.Forms.Button();
      this.button1 = new System.Windows.Forms.Button();
      this.comboBox1 = new System.Windows.Forms.ComboBox();
      this.datagrid1 = new System.Windows.Forms.DataGrid();
      ((System.ComponentModel.ISupportInitialize)
        (this.datagrid1)).BeginInit();
      this.SuspendLayout();
      // 

      // label1

      // 

      this.label1.Font = new System.Drawing.Font(
        "Microsoft Sans Serif", 8.25F, 
        System.Drawing.FontStyle.Bold, 
        System.Drawing.GraphicsUnit.Point, 
        ((System.Byte)(0)));
      this.label1.ForeColor = System.Drawing.Color.Brown;
      this.label1.Location = new System.Drawing.Point(24, 16);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(608, 23);
      this.label1.TabIndex = 8;
      this.label1.Text = 
        "Select the win32 API or type in a new one. 
        Then click on the \"Show\" button.";
      // 

      // label2

      // 

      this.label2.Font = new System.Drawing.Font(
        "Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, 
        System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.label2.ForeColor = System.Drawing.Color.Brown;
      this.label2.Location = new System.Drawing.Point(24, 72);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(136, 23);
      this.label2.TabIndex = 7;
      this.label2.Text = "Details";
      // 

      // button2

      // 

      this.button2.DialogResult = 
        System.Windows.Forms.DialogResult.Cancel;
      this.button2.Location = new System.Drawing.Point(600, 40);
      this.button2.Name = "button2";
      this.button2.TabIndex = 4;
      this.button2.Text = "Cancel";
      this.button2.Click += new System.EventHandler(
        this.button2_Click);
      // 

      // button1

      // 

      this.button1.Location = new System.Drawing.Point(504, 40);
      this.button1.Name = "button1";
      this.button1.TabIndex = 9;
      this.button1.Text = "Show";
      this.button1.Click += new System.EventHandler(
        this.button1_Click);
      // 

      // comboBox1

      // 

      this.comboBox1.Items.AddRange(new object[] {
        "Win32_DiskDrive",
          "Win32_OperatingSystem",
          "Win32_Processor",
          "Win32_ComputerSystem",
          "Win32_StartupCommand",
          "Win32_ProgramGroup",
          "Win32_SystemDevices"});
        this.comboBox1.Location = new System.Drawing.Point(24, 40);
        this.comboBox1.Name = "comboBox1";
        this.comboBox1.Size = new System.Drawing.Size(456, 21);
        this.comboBox1.TabIndex = 10;
        // 

        // datagrid1

        // 

        this.datagrid1.DataMember = "";
        this.datagrid1.HeaderForeColor = 
          System.Drawing.SystemColors.ControlText;
        this.datagrid1.Location = new System.Drawing.Point(24, 96);
        this.datagrid1.Name = "datagrid1";
        this.datagrid1.Size = new System.Drawing.Size(744, 264);
        this.datagrid1.TabIndex = 11;
        // 

        // Form1

        // 

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(784, 382);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.datagrid1);
        this.Controls.Add(this.comboBox1);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.button2);
        this.Icon = ((System.Drawing.Icon)(
          resources.GetObject("$this.Icon")));
        this.MaximizeBox = false;
        this.Name = "Form1";
        this.Text = "System Info";
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)
          (this.datagrid1)).EndInit();
        this.ResumeLayout(false);

    }
  #endregion

    private void Form1_Load(object sender, System.EventArgs e)
    {
    }
    private void button1_Click(object sender, System.EventArgs e)
    {

      datagrid1.DataSource = GetStuff(comboBox1.Text);
    }

    public ArrayList GetStuff(string queryObject)
    {
      ManagementObjectSearcher searcher;
      int i = 0;
      ArrayList hd = new ArrayList();
      try
      {
        searcher = new ManagementObjectSearcher(
          "SELECT * FROM " + queryObject);
        foreach(ManagementObject wmi_HD in searcher.Get())
        {
          i++;
          PropertyDataCollection searcherProperties = 
            wmi_HD.Properties;
          foreach (PropertyData sp in searcherProperties) 
          {
            hd.Add(sp);
          }
        }
      }
      catch(Exception ex)
      {
        MessageBox.Show(ex.ToString());
      }
      return hd;
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
      this.Close(); 
    }
  }
}

Points of Interest

This sample shows you how .NET wrapper around the basic win32 API have simplified the life for programmers. Now it is easy to access complete details about the system using System.Management namespace.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here