Click here to Skip to main content
15,880,956 members
Articles / Programming Languages / C#
Article

Get System Info using C#

Rate me:
Please Sign up or sign in to vote.
4.67/5 (41 votes)
9 Sep 20041 min read 236K   8.1K   89   14
An Article that shows how to use System.Management in C# to get System Information

Image 1

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
C#
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

C#
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...

C#
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


Written By
United States United States

Comments and Discussions

 
GeneralAn error occurred when opening Win32_SoftwareElement Pin
youyuming30-Dec-09 15:53
youyuming30-Dec-09 15:53 
GeneralQuestion about code Pin
Malindor30-Dec-09 6:39
Malindor30-Dec-09 6:39 
GeneralWonderfully usefull.....thanks for posting Pin
redware.com7-May-09 22:37
redware.com7-May-09 22:37 
GeneralDoes it work in Mac Pin
Member 337844217-Apr-09 9:34
Member 337844217-Apr-09 9:34 
GeneralVery good Pin
ryetwo24-Jan-08 4:01
ryetwo24-Jan-08 4:01 
GeneralGood work Pin
Wayne Phipps25-Feb-07 10:32
Wayne Phipps25-Feb-07 10:32 
GeneralInfo on a pocket PC Pin
alamatula14-Sep-06 15:45
alamatula14-Sep-06 15:45 
GeneralWhere do I find these System.Management Pin
Cheetah08152-Aug-06 9:30
Cheetah08152-Aug-06 9:30 
AnswerRe: Where do I find these System.Management Pin
Cool Cassis17-Aug-06 22:10
Cool Cassis17-Aug-06 22:10 
GeneralManagementObjectSearcher.Get() generic failure Pin
vinu_p3-May-06 23:27
vinu_p3-May-06 23:27 
QuestionFree space of a partition Pin
vicenç20-Nov-05 21:08
vicenç20-Nov-05 21:08 
JokeRe: Free space of a partition Pin
Stefan Prodan15-Feb-06 9:17
Stefan Prodan15-Feb-06 9:17 
GeneralGet System Info using C# Pin
s613510-Sep-04 2:52
s613510-Sep-04 2:52 
GeneralWin32_NetworkAdapterConfiguration Pin
BigByte10-Sep-04 6:28
BigByte10-Sep-04 6:28 

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

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