2 Replies - 308 Views - Last Post: 21 January 2013 - 11:12 AM Rate Topic: -----

#1 CodeMonkey65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-January 13

Getting hardware info via code

Posted 20 January 2013 - 08:33 AM

Hi all

I apologze if this has beena asked before, but I would like some tips on how to obtain hardware info on an installed machine, such as processor specs, hard drive size, video card capabilities, etc.

Thanks all
Is This A Good Question/Topic? 0
  • +

Replies To: Getting hardware info via code

#2 bgammill  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 39
  • Joined: 11-June 12

Re: Getting hardware info via code

Posted 21 January 2013 - 09:11 AM

View PostCodeMonkey65, on 20 January 2013 - 10:33 AM, said:

Hi all

I apologze if this has beena asked before, but I would like some tips on how to obtain hardware info on an installed machine, such as processor specs, hard drive size, video card capabilities, etc.

Thanks all

What you're looking for is called WMI, or Windows Management Instrumentation. Luckily for you, .NET provides a few ways to interact with WMI. The implementation is going to look a little awkward. Don't shoot the messenger.

First, add two references to your code:
System.Management
System.Management.Instrumentation



Second, add the appropriate using statements to your code:
using System.Management;
using System.Management.Instrumentation;



Next, you're going to want to create a ManagementObjectSearcher object. This will retrieve a ManagementObjectCollection based on your query parameters. The query string should contain one of the many WMI classes that abstract the specified hardware's data, as well as one or many of its properties. You can find a list of them here, a direct link to MSDN. For this example I'll be querying the network interfaces on my computer, by name.
ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher("Select Name from Win32_NetworkAdapter");
ManagementObjectCollection objectCollection = objectSearcher.Get();



Once you've successfully queried WMI, you can loop through your ManagementObjectCollection. This will allow you to look at each ManagementObject in the collection and loop through its properties.
            foreach (ManagementObject obj in objectCollection)
            {
                foreach (PropertyData prop in obj.Properties)
                {
                    Console.WriteLine(prop.Value);
                }
            }



For me this outputs:
WAN Miniport (SSTP)
WAN Miniport (IKEv2)
WAN Miniport (L2TP)
WAN Miniport (PPTP)
WAN Miniport (PPPOE)
WAN Miniport (IPv6)
WAN Miniport (Network Monitor)
Realtek PCIe GBE Family Controller
WAN Miniport (IP)
Microsoft ISATAP Adapter
RAS Async Adapter
Teredo Tunneling Pseudo-Interface
Hamachi Network Interface
Microsoft ISATAP Adapter #2
VirtualBox Bridged Networking Driver Miniport
VirtualBox Bridged Networking Driver Miniport
VirtualBox Host-Only Ethernet Adapter
Microsoft ISATAP Adapter #3



You're all set. Remember to follow the link I've included and play around with the query string. Try other classes and properties. Here's a link to the final code.
Was This Post Helpful? 4
  • +
  • -

#3 CodeMonkey65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-January 13

Re: Getting hardware info via code

Posted 21 January 2013 - 11:12 AM

Thanks for the great info!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1