Get System Info Using C# - CodeProject
Get System Info Using C# - CodeProject
10 Sep 2004
Download source - 8 Kb
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 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
1 of 7 16/10/2020, 20:19
Get System Info using C# - CodeProject https://www.codeproject.com/Articles/8241/Get-System-Info-using-C?di...
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.
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
2 of 7 16/10/2020, 20:19
Get System Info using C# - CodeProject https://www.codeproject.com/Articles/8241/Get-System-Info-using-C?di...
{
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 );
}
3 of 7 16/10/2020, 20:19
Get System Info using C# - CodeProject https://www.codeproject.com/Articles/8241/Get-System-Info-using-C?di...
((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"});
4 of 7 16/10/2020, 20:19
Get System Info using C# - CodeProject https://www.codeproject.com/Articles/8241/Get-System-Info-using-C?di...
}
#endregion
datagrid1.DataSource = GetStuff(comboBox1.Text);
}
5 of 7 16/10/2020, 20:19
Get System Info using C# - CodeProject https://www.codeproject.com/Articles/8241/Get-System-Info-using-C?di...
{
hd.Add(sp);
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return hd;
}
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.
Nitin Kunte
United States United States
6 of 7 16/10/2020, 20:19
Get System Info using C# - CodeProject https://www.codeproject.com/Articles/8241/Get-System-Info-using-C?di...
using-C
Permalink Article Copyright 2004 by Nitin Kunte
to
Advertise Everything else Copyright © CodeProject,
post
Privacy 1999-2020
and
Cookies
Terms
view of Use Web03 2.8.20201015.1
comments
on
this
article,
or
click
here
to
get
a
print
view
with
messages.
7 of 7 16/10/2020, 20:19