3

I've been trying to use System.Drawing.Printing; in order to get the queue status of a network printer.

I can retrieve the properties of the printer but I can't really seem to get the queue status.

This is what I've tried so far:

PrinterSettings ps = new PrinterSettings();
ps.PrinterName = "ES5461 MFP(PCL)"; // Load the appropriate printer's setting

From there I can see that the Printer is valid since ps.IsValid is true but I can't go any further.

I've tried as well to use System.Management to retrieve the status but I just know how to dump the information and there's no queue information as well.

string printerName = "ES5461";
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}%'", printerName);

ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection coll = searcher.Get();

foreach (ManagementObject printer in coll)
{
    foreach (PropertyData property in printer.Properties)
    {
        Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value));
    }
}

Do you know of any way to retrieve the queue status (number of documents) using any .dll?

Miquel Coll
  • 759
  • 15
  • 49
  • 1
    As from .NET 3.0 you can use the PrintQueue class. have a look at: https://msdn.microsoft.com/en-us/library/system.printing.printqueue(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/system.printing.printqueue.getjob(v=vs.110).aspx – Nissim Jun 16 '15 at 09:08
  • I already lookep that up but it is not recomended by Microsoft to use it in any ASP.Net services: "Classes within the System.Printing namespace are not supported for use within a Windows service or ASP.NET application or service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions." Thank you anyway. – Miquel Coll Jun 16 '15 at 09:23
  • 1
    According to microsoft, the System.Drawing.Printing namespace is to be used for winforms as well. – Nissim Jun 16 '15 at 09:28
  • @MiquelColl - why do you pay attention to that warning about `System.Printing` but ignore the similar warning for [`System.Drawing.Printing`](https://msdn.microsoft.com/en-us/library/system.drawing.printing(v=vs.110).aspx)? – Damien_The_Unbeliever Jun 16 '15 at 09:40
  • Oops. I didn't see that one. My fault. Sorry; Either way I'm trying to use what Nissim said again and it seems I'm getting to something. Thanks! – Miquel Coll Jun 16 '15 at 09:43

1 Answers1

3

Thanks to Nissim I could solve it:

var printServer = new PrintServer();
var myPrintQueues = printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });

foreach (PrintQueue pq in myPrintQueues)
{
    pq.Refresh();
    if (!pq.Name.ToLower().Contains("es5461")) continue;  
    PrintJobInfoCollection jobs = pq.GetPrintJobInfoCollection();
    foreach (PrintSystemJobInfo job in jobs)
    {
        var aux = job;
    }// end for each print job    
}// end for each print queue

As you can see using the PrintServer (System.Printing) combined with the PrintQueue as suggested by Nissim I can actually access to the queue information.

Miquel Coll
  • 759
  • 15
  • 49
  • 2
    I was just reading through this as I am working on a web service that does network printing, `PrintServer` is not in `System.Drawing.Printing` it is in `System.Printing` – Ryan Wilson Jul 18 '18 at 13:29
  • Thanks for the heads up @RyanWilson. Indeed the correct reference is in there. – Miquel Coll Jul 20 '18 at 08:44
  • Thank you for updating that. I just didn't want to see a good answer displaying incorrect info. Good post though. +1 from me. – Ryan Wilson Jul 20 '18 at 12:00