This Blog Has Been Moved !

This Blog Has been moved to http://aleemkhan.wordpress.com

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();

            }

     

      }

}

 

Comments

1 comments have been posted.