Sunday, May 20, 2007

Retrieving your Computer Information using WMI

Hey I am back again with a new post. Many people wonder how can I write a simple application that can tell me everything about my computer, my OS version, detect if I have a printer or not, ....... etc
Well, we will be using the WMI, Windows Management Windows Management Instrumentation. I believe the best way to discuss this topic is by observing the code directly as it is really simple to read and understand. Please feel free if you have any questions or comments to post them :)

Happy coding...

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace WMI
{
class Program
{
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Console.WriteLine("Computer details retrieved using Windows Management Instrumentation (WMI)");
Console.WriteLine("Written By Ahmed IG - ahmedsayed_86@hotmail.com");
Console.WriteLine("=========================================================================");
ManagementObjectSearcher query1 = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
ManagementObjectCollection queryCollection1 = query1.Get();
foreach (ManagementObject mo in queryCollection1)
{
Console.WriteLine("Name : " + mo["name"].ToString());
Console.WriteLine("Version : " + mo["version"].ToString());
Console.WriteLine("Manufacturer : " + mo["Manufacturer"].ToString());
Console.WriteLine("Computer Name : " + mo["csname"].ToString());
Console.WriteLine("Windows Directory : " + mo["WindowsDirectory"].ToString());
}

query1 = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
queryCollection1 = query1.Get();
foreach (ManagementObject mo in queryCollection1)
{
Console.WriteLine("Manufacturer : " + mo["manufacturer"].ToString());
Console.WriteLine("Model : " + mo["model"].ToString());
Console.WriteLine(mo["systemtype"].ToString());
Console.WriteLine("Total Physical Memory : " + mo["totalphysicalmemory"].ToString());
}

query1 = new ManagementObjectSearcher("SELECT * FROM Win32_processor");
queryCollection1 = query1.Get();
foreach (ManagementObject mo in queryCollection1)
{
Console.WriteLine(mo["caption"].ToString());
}

query1 = new ManagementObjectSearcher("SELECT * FROM Win32_bios");
queryCollection1 = query1.Get();
foreach (ManagementObject mo in queryCollection1)
{
Console.WriteLine(mo["version"].ToString());
}

query1 = new ManagementObjectSearcher("SELECT * FROM Win32_timezone");
queryCollection1 = query1.Get();
foreach (ManagementObject mo in queryCollection1)
{
Console.WriteLine(mo["caption"].ToString());
}

}
}
}

A good refrence about the available Win32 classes, please visit http://msdn2.microsoft.com/en-us/library/aa394084.aspx

2 comments:

  1. Really great post, but I wonder, Is there any method that you can get all the curent info about all win32 classes in your Windows?, I noticed that you 've used a foreach loop for every class you want to view it's info, Is this possible ?

    ReplyDelete
  2. Thanks for reading and your feedback.
    Well this is not possible, you must at least identify the class that you want to search in, you can then select all info in that class. In other words, you must explicitly identify the class.
    For a complete list of all Win32 classes that are available, please visit:
    http://msdn2.microsoft.com/en-us/library/aa394084.aspx

    Thank you...

    ReplyDelete