Csharp/C Sharp/Development Class/WMI
Computer details retrieved using Windows Management Instrumentation (WMI)
//WMI.cs
//Extracts computer system information using WMI from Microsoft
//Written 01/17/02 John O"Donnell - csharpconsulting@hotmail.ru
using System;
using System.Management;
namespace WMI
{
/// <summary>
/// Summary description for WMI.
/// </summary>
public class WMI
{
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 01/17/02 By John O"Donnell - csharpconsulting@hotmail.ru");
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());
}
}
}
}
Management Object Searcher
using System;
using System.Management;
public class WMICardGrab
{
public static void Main()
{
ManagementObjectSearcher query = new
ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = "TRUE"") ;
ManagementObjectCollection queryCollection = query.Get();
foreach( ManagementObject mo in queryCollection )
{
string[] addresses = (string[])mo["IPAddress"];
string[] subnets = (string[])mo["IPSubnet"];
string[] defaultgateways = (string[])mo["DefaultIPGateway"];
Console.WriteLine("Network Card: {0}", mo["Description"]);
Console.WriteLine(" MAC Address: {0}", mo["MACAddress"]);
foreach(string ipaddress in addresses)
{
Console.WriteLine(" IP Address: {0}", ipaddress);
}
foreach(string subnet in subnets)
{
Console.WriteLine(" Subnet Mask: {0}", subnet);
}
foreach(string defaultgateway in defaultgateways)
{
Console.WriteLine(" Gateway: {0}", defaultgateway);
}
}
}
}