Using System;
Using System. Management; // also needs to be referenced;
Using System. Collections;
Using System. Collections. Specialized;
Using System. Text;
Namespace Rainsoft. Management
{
# Region WMIPath
Public enum WMIPath
{
// Hardware
Win32_Processor, // CPU Processor
Win32_PhysicalMemory, // physical memory
Win32_Keyboard, // keyboard
Win32_PointingDevice, // enter the device, including the mouse.
Win32_FloppyDrive, // floppy disk drive
Win32_DiskDrive, // hard drive
Win32_CDROMDrive, // Optical Drive
Win32_BaseBoard, // Motherboard
Win32_BIOS, // BIOS chip
Win32_ParallelPort, // parallel port
Win32_SerialPort, // serial port
Win32_SerialPortConfiguration, // serial port configuration
Win32_SoundDevice, // multimedia settings, usually the sound card.
Win32_SystemSlot, // motherboard slot (ISA & PCI & AGP)
Win32_USBController, // USB controller
Win32_NetworkAdapter, // Network Adapter
Win32_NetworkAdapterConfiguration, // network adapter settings
Win32_Printer, // printer
Win32_PrinterConfiguration, // printer settings
Win32_PrintJob, // printer task
Win32_TCPIPPrinterPort, // printer port
Win32_POTSModem, // MODEM
Win32_POTSModemToSerialPort, // MODEM Port
Win32_DesktopMonitor, // display
Win32_DisplayConfiguration, // graphics card
Win32_DisplayControllerConfiguration, // video card settings
Win32_VideoController, // video card details.
Win32_VideoSettings, // Display Mode Supported by the video card.
// Operating System
Win32_TimeZone, // Time Zone
Win32_SystemDriver, // driver
Win32_DiskPartition, // disk partition
Win32_LogicalDisk, // Logical Disk
Win32_LogicalDiskToPartition, // partition and start and end location of the Logical Disk.
Win32_LogicalMemoryConfiguration, // logical memory configuration
Win32_PageFile, // system page file information
Win32_PageFileSetting, // page File Settings
Win32_BootConfiguration, // system startup configuration
Win32_ComputerSystem, // brief Computer Information
Win32_OperatingSystem, // operating system information
Win32_StartupCommand, // the system automatically starts the program
Win32_Service, // system-installed Service
Win32_Group, // System Management Group
Win32_GroupUser, // system group account
Win32_UserAccount, // User Account
Win32_Process, // system process
Win32_Thread, // system thread
Win32_Share, // share
Win32_NetworkClient, // installed network client
Win32_NetworkProtocol, // installed network protocol
}
# Endregion
/// <Summary>
/// Obtain system information
/// </Summary>
/// <Example>
/// <Code>
/// WMI w = new WMI (WMIPath. Win32_NetworkAdapterConfiguration );
/// For (int I = 0; I <w. Count; I ++)
///{
/// If (bool) w [I, "IPEnabled"])
///{
/// Console. WriteLine ("Caption: {0}", w [I, "Caption"]);
/// Console. WriteLine ("MAC Address: {0}", w [I, "MACAddress"]);
///}
///}
/// </Code>
/// </Example>
Public sealed class WMI
{
Private ArrayList mocs;
Private StringDictionary names; // used to store attribute names, so that correct names can be queried regardless of case.
/// <Summary>
/// Number of information sets
/// </Summary>
Public int Count
{
Get {return mocs. Count ;}
}
/// <Summary>
/// Obtain the specified property value. Note that some results may be arrays.
/// </Summary>
Public object this [int index, string propertyName]
{
Get
{
Try
{
String trueName = names [propertyName. Trim ()]; // you can obtain the correct attribute name in case insensitive.
Hashtable h = (Hashtable) mocs [index];
Return h [trueName];
}
Catch
{
Return null;
}
}
}
/// <Summary>
/// Return all attribute names.
/// </Summary>
/// <Param name = "index"> </param>
/// <Returns> </returns>
Public string [] PropertyNames (int index)
{
Try
{
Hashtable h = (Hashtable) mocs [index];
String [] result = new string [h. Keys. Count];
H. Keys. CopyTo (result, 0 );
Array. Sort (result );
Return result;
}
Catch
{
Return null;
}
}
/// <Summary>
/// Return the test information.
/// </Summary>
/// <Returns> </returns>
Public string Test ()
{
Try
{
StringBuilder result = new StringBuilder (1000 );
For (int I = 0; I <Count; I ++)
{
Int j = 0;
Foreach (string s in PropertyNames (I ))
{
Result. Append (string. Format ("{0 }:{ 1 }={ 2} \ n", ++ j, s, this [I, s]);
If (this [I, s] is Array)
{
Array v1 = this [I, s] as Array;
For (int x = 0; x <v1.Length; x ++)
{
Result. Append ("\ t" + v1.GetValue (x) + "\ n ");
}
}
}
Result. Append ("====== WMI =========\ n ");
}
Return result. ToString ();
}
Catch
{
Return string. Empty;
}
}
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "path"> </param>
Public WMI (string path)
{
Names = new StringDictionary ();
Mocs = new ArrayList ();
Try
{
ManagementClass cimobject = new ManagementClass (path );
ManagementObjectCollection moc = cimobject. GetInstances ();
Bool OK = false;
Foreach (ManagementObject mo in moc)
{
Hashtable o = new Hashtable ();
Mocs. Add (o );
Foreach (PropertyData p in mo. Properties)
{
O. Add (p. Name, p. Value );
If (! OK) names. Add (p. Name, p. Name );
}
OK = true;
Mo. Dispose ();
}
Moc. Dispose ();
}
Catch (Exception e)
{
Throw new Exception (e. Message );
}
}
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "path"> </param>
Public WMI (WMIPath path)
: This (path. ToString ())
{
}
}
}