How to get information about all the printers that are installed in my windows machine from a C# program via WMI

I need to create a windows service that is able to send some print jobs to one of the installed printers. As this is a windows service, it has to be able to find the printer with a preset printer name. This post details some prototyping codes that I had created to determine whether I am able to locate information of all the printers that are installed in the windows host where my windows service will run.

The WMI query to find all information of all the printers on my computer

I recalled that when I tried to find the current login user with WMI previously, I saw the Win32_Printer class in my Windows Management Instrumentation Tester.

I first try to see if the Win32_Printer class is able to provide me with information of the printers that are installed in my windows machine with my Windows Management Instrumentation Tester. To see all the printers that are installed in my windows machine with WMI, I connect to the root\cimv2 namespace and run the following WMI query:

Select * from Win32_printer 

Indeed, with the above query, I could retrieve information of all my printers that I had installed in my windows machine.

The C# codes that can get information about all the printers that are installed in my windows machine

After verifying that the WMI query works with my Windows Management Instrumentation Tester, I proceeded to write the C# codes to retrieve information of my all printers that are installed in my windows machine:

ManagementScope ms = new ManagementScope("\\\\.\\root\\cimv2");
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, query);
foreach (ManagementObject mo in searcher.Get())
{
    Console.WriteLine(mo["Name"].ToString());
}

I first create a ManagementScope instance that points to the root\cimv2 namespace and a ObjectQuery instance that holds the query to search for all the printers that I had installed. I then create the ManagementObjectSearcher object to perform the search. To print out the names of the printer, I iterate the ManagementObject instances found in the ManagementObjectCollection returned from a call to searcher.Get(). I then write the value of the name property of each ManagementObject instance to console.

To get more information about the printers, we can refer to the Win32_Printer class documentation for the available property values.

An additional step needed for my windows service to get information about printers installed in my windows machine.

After I saw my c# codes in my console application project picking up the names of the printers that are installed in my windows machine, I put them into a windows service project. However, I realized that my windows service was not able to pick up the name of any of the printers installed in my windows machine.

I remembered that different users in windows are assigned separate sets of printer installations. As such, to ensure that my windows service sees my set of printer installation, I had to configure it to logon as my local user account via my services management console.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.