C # obtain computer hardware configuration information

Source: Internet
Author: User
Using system;
Using system. Management;
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 ())
{
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.