Getting Printer Information through WMI

Hey,

I just got a mail from one of the friends, he wants to get the information about the printer like printer status, error, current status etc, I thought I should share the answer with all.

Yes there are ways to do this, the most common answer to almost anything related to any information in Windows is

WMI (Windows Management Instrumentation), this is the layer windows provides and uses for retrieving all sorts of information from the system and the running kernel.

In the following code I have listed all your printers on the system and also some of their properties/status like printing, stopped etc, but I can’t possibly list everything.

There is tons of information you can get about printer using WMI Win32_Printer class. Please refer to

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_printer.asp and modify this code according to what you actually need.

using System;

using System.Diagnostics;

using System.Management;

using System.ComponentModel;

namespace ConsoleWMI

{

class Class1

      {

            [STAThread]

static void Main(string[] args)

            {

new Class1();

            }

public Class1()

            {

                  System.Management.ManagementObjectSearcher searcher = new ManagementObjectSearcher

                        (“SELECT * FROM Win32_Printer”);

foreach (ManagementObject service in searcher.Get())

                  {

string [] pStatus = {“Other”,”Unknown”,”Idle”,”Printing”,”WarmUp”,”Stopped Printing”, “Offline”};

string [] pState = {“Paused”,”Error”,”Pending Deletion”,”Paper Jam”,”Paper Out”,”Manual Feed”,”Paper Problem”,

                                                         “Offline”,”IO Active”,”Busy”,”Printing”,”Output Bin Full”,”Not Available”,”Waiting”,

                                                         “Processing”,”Initialization”,”Warming Up”,”Toner Low”,”No Toner”,”Page Punt”,

                                                         “User Intervention Required”,”Out of Memory”,”Door Open”,”Server_Unknown”,”Power Save”};

foreach(System.Management.PropertyData pData in service.Properties)

                              {

if(pData.Name == “Caption” || pData.Name == “Default” || pData.Name==”ServerName”)

                                          Console.WriteLine(pData.Name + ” = ” + pData.Value);

else if(pData.Name == “PrinterState”)

                                          Console.WriteLine(pData.Name + ” = ” + pState[Convert.ToInt32(pData.Value)]);

else if(pData.Name == “PrinterStatus”)

                                          Console.WriteLine(pData.Name + ” = ” + pStatus[Convert.ToInt32(pData.Value)]);

                              }

                              Console.WriteLine();

                  }

                  Console.ReadLine();

            }

      }

}

About these ads

14 thoughts on “Getting Printer Information through WMI

  1. Hi,
    I want to retreive the status of printer connected in a network which is a usb printer.i have created a windows service which runs and generates reports for the customer and then prints.but if the printer is not ready or not connected then it should not generate the report and the reason should be told to the customer abt the printer.
    can i get the code for this in c#.net??
    Thanking You

  2. hello, i want to get the copy of the page the printer is printing… like i am printing 5 copies of this page, how do i get the counter that will count if the current printing job is “Copy 1 of 5″, “Copy 2 of 5″ and so on, tnx

  3. I have a problem, i hope you can help with it.

    I use the fuction that you give but i dont kwon how it reale works.

    the state of the printer sometimes send 1024, 1025 just when a send a print job and i remove the paper on the printer. The state dont send a value if a idont send a print job, is that ok ??

    and also send values unspected.

    i hope you can give a help with a state of the printer.

  4. Nice blog about printer. I have a lot of problem with my office printer. I’m IT support and need to solve any problem. Do you have any suggestion about printer troushooting guide site? Thank you very much.

  5. could you please tell me how can i get the owner document ip if that owner is one of network machines

    when i search using win32_printerjob and viwe the owner filed it is appear “Guest” and i want to know the ip address of that guest

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s