3

I am new using WMI. What is it?

Can I use WMI call in C# for example to get list of drivers on my PC? If so, which class do I call?

RRUZ
  • 134,889
  • 20
  • 356
  • 483
user1683517
  • 171
  • 3
  • 7
  • http://geekswithblogs.net/PsychoCoder/archive/2008/01/25/using_wmi_in_csharp.aspx and this......... http://www.dlssoftwarestudios.com/simple-wmi-with-c/ – riti Oct 30 '12 at 04:39

1 Answers1

6

To list the installed drivers you can use the Win32_PnPSignedDriver WMI class as is show on this sample.

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

namespace GetWMI_Info
{
    class Program
    {


        static void Main(string[] args)
        {
            try
            {
                string ComputerName = "localhost";
                ManagementScope Scope;                
                Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_PnPSignedDriver");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Console.WriteLine("{0,-35} {1,-40}","ClassGuid",WmiObject["ClassGuid"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","DeviceClass",WmiObject["DeviceClass"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","DeviceID",WmiObject["DeviceID"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","DeviceName",WmiObject["DeviceName"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","Manufacturer",WmiObject["Manufacturer"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","Name",WmiObject["Name"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","Status",WmiObject["Status"]);// String

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}

Also if you are new in the WMI topic you can use a tool like the WMI Delphi Code Creator to explore the WMI contents and generate code to access the WMI.

RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • Thank you for your great example. I just don't understand what this line means. What if I just want to print the value and not provider string. Do I remove the {0, -35} portion together with "Provider" string? Console.WriteLine("{0,-35} {1,-40}", "Provider", WmiObject["Manufacturer"]); – user1683517 Oct 31 '12 at 05:08
  • 1
    If you want only show the value of the WMI property, you can use this `Console.WriteLine(WmiObject["Manufacturer"]);` anyway the posted code is a console sample app, if you want more info about this topic try the documentation about the [Console.WriteLine](http://msdn.microsoft.com/en-us/library/system.console.writeline.aspx) method, also if this answer help you consider [accept the answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – RRUZ Oct 31 '12 at 05:25
  • Last question: how can I get list of all available properties? I want to see what else I can use other than the ones you have listed? – user1683517 Oct 31 '12 at 18:44
  • just read the documentation of the `Win32_PnPSignedDriver` class http://msdn.microsoft.com/en-us/library/windows/desktop/aa394354%28v=vs.85%29.aspx – RRUZ Nov 02 '12 at 04:39