Click here to Skip to main content
6,629,885 members and growing! (23,809 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Get System Info using C#

By Nitin Kunte

An Article that shows how to use System.Management in C# to get System Information
C#, Windows, .NET 1.0, Visual Studio, Dev
Posted:9 Sep 2004
Views:88,484
Bookmarked:60 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
26 votes for this article.
Popularity: 5.70 Rating: 4.03 out of 5
3 votes, 11.5%
1

2

3
9 votes, 34.6%
4
14 votes, 53.8%
5

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

About the Author

Nitin Kunte


Member
By Mind Experts
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralWonderfully usefull.....thanks for posting Pinmemberredware.com23:37 7 May '09  
GeneralDoes it work in Mac PinmemberMember 337844210:34 17 Apr '09  
GeneralVery good Pinmemberryetwo5:01 24 Jan '08  
GeneralGood work PinmemberWayne Phipps11:32 25 Feb '07  
GeneralInfo on a pocket PC Pinmemberalamatula16:45 14 Sep '06  
GeneralWhere do I find these System.Management PinmemberCheetah081510:30 2 Aug '06  
AnswerRe: Where do I find these System.Management PinmemberCool Cassis23:10 17 Aug '06  
GeneralManagementObjectSearcher.Get() generic failure Pinmembervinu_p0:27 4 May '06  
QuestionFree space of a partition Pinmembervicenç22:08 20 Nov '05  
JokeRe: Free space of a partition PinmemberStefan Prodan10:17 15 Feb '06  
GeneralGet System Info using C# Pinmembers61353:52 10 Sep '04  
GeneralWin32_NetworkAdapterConfiguration PinmemberBigByte7:28 10 Sep '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Sep 2004
Editor: Nishant Sivakumar
Copyright 2004 by Nitin Kunte
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project