Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there

I am trying to get the current state of a printer. I have this code below but its just returning one value, "3" (Even if the printer is not plugged in to my laptop). I an new to WMI so if anyone can spot an error that I have done it would be much appreciated.
Basically im just trying to return the different states in number format and then I can write out the states. I am using VS 2008.

Thanks


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace printerWMITestAgain
{
   class Program
   {
      static void Main(string[] args)
      {
         string status;

         ConnectionOptions oConn = new ConnectionOptions();

         ManagementScope oMs = new ManagementScope("\\\\EDREN-THINK\\root\\cimv2", oConn);

         oMs.Connect();

         ObjectQuery oQuery = new ObjectQuery("SELECT PrinterStatus FROM Win32_Printer WHERE Name =\"HP psc 2500 Series\"");

         ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);

         ManagementObjectCollection oReturnCollection = oSearcher.Get();

         foreach (ManagementObject oReturn in oReturnCollection)
         {
            status = oReturn["PrinterStatus"].ToString();
            Console.WriteLine(status);
            Console.ReadLine();
         }
      }
   }
}
Posted
Updated 13-May-10 21:59pm
v3

This may be of some help.

C#
enum PrinterStatus
{
  Other = 1,
  Unknown,
  Idle,
  Printing,
  Warmup,
  Stopped Printing,
  Offline
}


So the result that you are getting (3) means it is Idling.

I suspect that you might have already seen it but just in case, here[^] is the MSDN page for Win32_Printer.

There is also an article WMI Interface for .NET[^] here on Code Project that may be of some help.

Good luck! :)
 
Share this answer
 
Comments
Uros Calakovic 14-May-10 12:26pm    
I don't think that property will change even if the printer is turned off (or incapable of printing for another reason) until a document is sent to it and failed to print. For example, if I create a bogus printer on my machine, it is reported as an instance of Win32_Printer and its PrinterStatus is equal to 3 (Idle) until I try to print something. Then it changes to 1 (Other). So, this is probably not the best way to check printer status. I'm not sure about other ways, maybe checking the list of PnP devices.
edren06 16-May-10 10:55am    
Hi thank you very much for the help. Yes I have looked at the Win32_Printer classes and managed to get some different results now. I am still learning a lot and I have been mainly studying VB until about 2 weeks ago when i started working at a company for free which is using c#

What exactly is the purpose of enum PrinterStatus?
edren06 16-May-10 10:56am    
At the end of the day i want my program to show the printer status and also show the if it is low on paper.
From what I have read on t'interwebs whilst researching this it seems that what information gets passed to WMI depends on the writers of the Printer Driver. Not sure how that works, but it may be that the information is not available though WMI for your printer.

Good luck! :)
 
Share this answer
 
The best way to get printer status is to use snmp. Its accurate and timely.
The MIB OIDs for printer status, page count, etc can be found here:

http://www.alvestrand.no/objectid/1.3.6.1.2.1.43.html[^]


use a free MIB browser to view the values available in your printer (I only use a few that are generic enough that all printers implement):

http://www.ireasoning.com/downloadmibbrowserfree.php[^]

Here is a sample code snippet I use.



C#
//free snmp library from codeplex
using Lextm.SharpSnmpLib.Messaging;
using Lextm.SharpSnmpLib;
using System.Net;

class Program {

    static string printerIP = "10.28.40.80"; //PBA01 - laser printer        
    static PrinterStatusObj printerStatus = new PrinterStatusObj();

    static void Main(string[] args) {
        GetSNMPPrinterStatus();
            Console.WriteLine("printer status is: " + printerStatus.PrinterStatus);
            Console.WriteLine("total page count is is: " + printerStatus.TotalPageCount);
            Console.ReadLine(); 
    }

    static void GetSNMPPrinterStatus() {
            printerStatus = new PrinterStatusObj();
            //string jobNameOID = "1.3.6.1.4.1.11.2.3.9.4.2.1.1.6.5.1";
            //string errrorStateOID = "1.3.6.1.2.1.25.3.5.1.2.1";
            //string alertDescOID = "1.3.6.1.2.1.43.18.1.1.8.1";

            string deviceStatusOID = "1.3.6.1.2.1.25.3.2.1.5.1";
            string printerStatusOID = "1.3.6.1.2.1.25.3.5.1.1.1";
            string totalPageCountOID = "1.3.6.1.2.1.43.10.2.1.5.1.1";



            var result = Messenger.Get(VersionCode.V1,
                           new IPEndPoint(IPAddress.Parse(printerIP), 161),
                           new OctetString("public"),
                           new List<Variable> {
                                            new Variable(new ObjectIdentifier(deviceStatusOID)),
                                            new Variable(new ObjectIdentifier(printerStatusOID)),
                                            new Variable(new ObjectIdentifier(totalPageCountOID))
                                            //new Variable(new ObjectIdentifier(errrorStateOID)), //not working
                                            //new Variable(new ObjectIdentifier(jobNameOID)), //not working
                                            //new Variable(new ObjectIdentifier(alertDescOID)) //not working
                                               },
                           60000);

            if (result.Count > 0) {
                printerStatus = new PrinterStatusObj() {
                    DeviceStatus = result[0].Data.ToString(),
                    PrinterStatus = result[1].Data.ToString(),
                    TotalPageCount = result[2].Data.ToString(),
                    //ErrorState = (byte)(result[3].Data.ToString().Trim() == "" ? 0 : Convert.ToInt32(result[2].Data.ToString().Trim())),                    //JobName = result[4].Data.ToString(),
                    //AlertDesc = result[5].Data.ToString()
                };

                int statuscode = 10 * Convert.ToInt32(printerStatus.DeviceStatus) + Convert.ToInt32(printerStatus.PrinterStatus);

                switch(statuscode) {
                    case 21:
                    case 23:
                        printerStatus.PrinterStatus = "PRINTER_READY";
                        break;
                    case 24:
                        printerStatus.PrinterStatus = "PRINTER_PRINTING";
                        break;
                    case 33:
                        if( (printerStatus.ErrorState & 2) != 2) { //not offline
                            printerStatus.PrinterStatus = "PRINTER_READY";
                        }
                        break;
                    case 34:
                        if( (printerStatus.ErrorState & 2) != 2)  { //not offline
                            printerStatus.PrinterStatus = "PRINTER_PRINTING";
                        }
                        break;
                    case 55:
                        printerStatus.PrinterStatus = "PRINTER_READY";
                        break;
                    default:
                        printerStatus.PrinterStatus = "PRINTER_NOT_READY";
                        break;
                }
            }
        }

 class PrinterStatusObj {
        public string JobName = "";
        public string DeviceStatus = "0";
        public string PrinterStatus = "0";
        public string TotalPageCount = "0";
        public byte ErrorState = 0;
        public string AlertDesc = "";
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900