question

GeraldOakham-9565 avatar image
0 Votes"
GeraldOakham-9565 asked LimitlessTechnology-2700 answered

printerQuery returns "unknown" status for each printer

hi,

I'm trying to write a program that will, in real-time, look at the status of all the printers installed on a machine, and if certain ones are seen, then perform an action.

I am approaching this step by step, so I am currently trying to get a list of all the installed printers, and their current status.

I can get the details using printerQuery, but every printer is listed as unknown .

         public static void GetWindowsPrinters(RichTextBox RTBName)
             {
             var printerQuery = new ManagementObjectSearcher("SELECT * from Win32_Printer");
             foreach (var printer in printerQuery.Get())
                 {
                 var _name = printer.GetPropertyValue("Name");
                 var _status = printer.GetPropertyValue("Status");
                 var _isDefault = printer.GetPropertyValue("Default");
                 var _isNetworkPrinter = printer.GetPropertyValue("Network");
                 var _portName = printer.Properties["PortName"].Value;
    
                 RTBName.AppendText(_name + " -- Status: " + _status + ", Default: " + _isDefault + ", Port: " + _portName + Environment.NewLine + " --------- " + Environment.NewLine);
    
                 if (_isNetworkPrinter.ToString() == "true")
                     {
                     var searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_TCPIPPrinterPort where Name LIKE '" + _portName + "'");
                     var results2 = searcher2.Get();
                     RTBName.AppendText(Environment.NewLine + "PortName:" + _portName + " PortNumber:" + results2.ToString() + Environment.NewLine + Environment.NewLine);
                     }
                 }
             }

Could someone point out where I am going wrong? why is everything coming back unknown ? I have 1 printer that windows 10 says are "not connected" (correct) and one that says it's "Not Available" (again, correct as it's off), but the Microsoft XPS document writer, FAX, Onenote etc are all "online".

I thought this was just me coding wrongly, so I tried to skip a few steps and use a watcher on the printers, but this fails also.

         public void listprinters()
             {
             string wqlQuery = @"SELECT * from Win32_Printer";
             WqlEventQuery query = new WqlEventQuery(wqlQuery);
             watcher = new ManagementEventWatcher(query);
             watcher.EventArrived +=
             new EventArrivedEventHandler(onEvent);
             watcher.Start();
         }
    
         public void onEvent(object sender, EventArrivedEventArgs e)
             {
             MessageBox.Show("Caught the Status" +"Printer Status");
             watcher.Stop();
             }

but when I invoke this, I get an unhandled exception @ the watcher.start line: - System.Management.ManagementException: 'Class is not an event class. '

Any help is always gratefully received.

thank you in advance

windows-server-print
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hello @GeraldOakham-9565,

Thank you for your question.

Please follow these steps, it will help you:

1 Check printer is online

private static bool IsOnline(ManagementBaseObject printer)
{
bool isOnlineprinter = true;
PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
var PrinterName = printerNative.GetPrinterName();
var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();
var ResultPrinter01 = printer.Properties["ExtendedPrinterStatus"].Value.ToString();
var ResultPrinter02 = printer.Properties["PrinterState"].Value.ToString();

         if (PrinterNameProperty == PrinterName)
         {
             //(no internet connection or printer switched off):PrinterState
             if (ResultPrinter02 == "128"|| ResultPrinter02=="4096")
             {
                 isOnlineprinter = false;
             }



             ////printer is initializing....
             //if (ResultPrinter02 == "16")
             //{
             //    isOnlineprinter = false;
             //}

             //(no internet connection or printer switched off):ExtendedPrinterStatus
             if (ResultPrinter01 == "7")
             {
                 isOnlineprinter = false;
             }

         }

         return isOnlineprinter;
      }

2 check for out of paper

private static bool IspaperOK(ManagementBaseObject printer)
{
bool PaperOK = true;

         PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
         var PrinterName = printerNative.GetPrinterName();
         var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();

         var PaperStatus = printer.Properties["PrinterState"].Value.ToString();
            
         if (PrinterNameProperty == PrinterName)
         {
             //(PrinterState)16 = Out of Paper
             //(PrinterState)5 = Out of paper
             //(PrinterState)4 = paperjam
             //(PrinterState)144 = Out of paper
             if ((PaperStatus == "5") || (PaperStatus == "16")||(PaperStatus=="144"))
             {
                 PaperOK = false;
             }

         }

         return PaperOK;
     }

3 Verify still printing state or not

private static bool Isprinting(ManagementBaseObject printer)
{
bool Isprintingnow = false;
PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
var PrinterName = printerNative.GetPrinterName();
var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();
var printing01 = printer.Properties["PrinterState"].Value.ToString();
var printing02 = printer.Properties["PrinterStatus"].Value.ToString();
if (PrinterNameProperty == PrinterName)
{
//(PrinterState)11 = Printing
//(PrinterState)1024 = printing
//(PrinterStatus)4 = printing
if (printing01 == "11" || printing01 == "1024" || printing02=="4")
{
Isprintingnow = true;
}

         }

         return Isprintingnow;
     }




4 check for error (Printer)

private static bool IsPrinterError(ManagementBaseObject printer)
{
bool PrinterOK = true;

         PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
         var PrinterName = printerNative.GetPrinterName();
         var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();

         var PrinterSpecificError = printer.Properties["PrinterState"].Value.ToString();
         var otherError = printer.Properties["ExtendedPrinterStatus"].Value.ToString();
         if (PrinterNameProperty == PrinterName)
         {
             //(PrinterState)2 - error of printer
             //(PrinterState)131072 - Toner Low
             //(PrinterState)18 - Toner Low
             //(PrinterState)19 - No Toner

             if ((PrinterSpecificError == "131072")||(PrinterSpecificError == "18")||(PrinterSpecificError == "19")||(PrinterSpecificError == "2")||(PrinterSpecificError == "7"))
             {
                 PrinterOK = false;
             }

             //(ExtendedPrinterStatus) 2 - no error
             if (otherError=="2")
             {
                 PrinterOK = true;
             }
             else
             {
                 PrinterOK = false;
             }

         }
         return PrinterOK;
     }



 5  check Network or USB

private static bool IsNetworkPrinter(ManagementBaseObject printer)
{
bool IsNetwork = true;
PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
var PrinterName = printerNative.GetPrinterName();
var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();

     var network = printer.Properties["Network"].Value.ToString();                   
     var local = printer.Properties["Local"].Value.ToString();

                 if (PrinterNameProperty == PrinterName)
                 {
                     if (network == "True")
                     {
                         IsNetwork = true;
                     }

                     if (network == "True" && local == "True")
                     {
                         IsNetwork = true;
                     }

                     if (local == "True" && network=="False")
                     {
                         IsNetwork = false;
                     }
                 }

     return IsNetwork;
 }






If the reply was helpful, please don't forget to upvote or accept as answer.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.