Search code examples
c#serviceprintingprintqueue

PrintQueue.Refresh don't show the status of printer


I need to run a Windows Service of server X to show the status of all printers: Out of paper, no toner, etc. The service is running on a machine but of course not all printers are installed on it. Even when the printers are installed on the machine, we do NOT have the status of the printers!

The only thing I was able to do is to remove the paper, print a test page (notepad), and now I can see that I'm missing paper with the code below, but as you might thing, this is not doable: I don't want to send a test page to every printers of the network every 10 minutes or so!

I try to query PrintQueue.Refresh but status is not updating, I don't see that the printer tray is open (or missing paper, or no toner, whatever I do with the printer.) BTW, Win32_printer don't show me a better result.

NOTE:

  1. MonitoringWS is the web service that can access the database.
  2. Printers is the list of printers that we want to query.

This is what I try to do.

 var printServers = GetListOfPrinterServers();
            var listPrinters = printers as List<Printer> ?? printers.ToList();
            foreach (
                var printServer in
                    printServers.Select(
                        server => new PrintServer(server, PrintSystemDesiredAccess.EnumerateServer)))
            {
                printServer.Refresh();
                var printQueues = printServer.GetPrintQueues();
                foreach (var printQueue in printQueues)
                {
                    var queue = printQueue;
                    var printersFound = listPrinters.Where(p =>
                                                                                                                                string.Equals(p.PrinterName, queue.FullName,
                                                                             StringComparison.OrdinalIgnoreCase));
                    foreach (var printer in printersFound)
                    {
                        printQueue.Refresh();

                        Debug.WriteLine(string.Format("{0} {1}", printQueue.FullName, printQueue.HostingPrintServer.Name) );
                        var pm = new MonitoringWS.PrinterMonitoring
                                     {
                                         FkPrinter = printer.PkPrinter,
                                         QueueStatus = printQueue.QueueStatus,
                                         DriverName = printQueue.QueueDriver.Name,
                                         MonitoringDateTime = DateTime.Now
                                     };

                        printerMonitorings.Add(pm);
                    }
                }
            }

Solution

  • I found a way: SNMP. I use library SNMP#NET and I implement RFC 2790: https://www.rfc-editor.org/rfc/rfc2790 .

    With that, when the printer support that standard and when SNMP is active, I got the status of the printer (no toner, no paper, paper jam, etc.)

    Thanks everybody for your help.