11

I'm looking for a way to get a list or number of jobs from a particular printer. In the best case I would want to have a "Job object" that represents one print job and its name in the print queue.

This is required because I need to monitor the state of a printer so I can refill the print queue with a new batch of documents without overflowing the print spooler

Thanks in advance!

Edit: added code fragment of solution

private int GetNumberOfPrintJobs()
{
    LocalPrintServer server = new LocalPrintServer();
    PrintQueueCollection queueCollection = server.GetPrintQueues();
    PrintQueue printQueue = null;

    foreach (PrintQueue pq in queueCollection)
    {
        if (pq.FullName == PrinterName)
            printQueue = pq;
    }

    int numberOfJobs = 0;
    if (printQueue != null)
        numberOfJobs = printQueue.NumberOfJobs;

    return numberOfJobs;
}
Berry Ligtermoet
  • 851
  • 2
  • 7
  • 24

2 Answers2

10

You can use the .NET 3.0 PrintQueue class in the System.Printing namespace. Its NumberOfJobs property tells you how many jobs are queued, GetPrintJobInfoCollection() returns details on all the jobs. Beware that it doesn't have any events that tells you that the job collection changed, you need to poll with a timer. Once a second or so ought to be fine.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I've had a look at this one already. But it requires a PrintServer object to instantiate. I'm using printers that are installed locally. Would this pose a problem? But this would certainly be an ideal solution. I already have a while() construction in place to poll as I'm printing in a thread and I need a blocking construction. Also this printer is _very_ slow ;) – Berry Ligtermoet Apr 08 '11 at 13:30
  • 4
    Use the LocalPrintServer class. – Hans Passant Apr 08 '11 at 13:42
  • Could it be that obvious? Thanks for showing me what Google couldn't come up with :) – Berry Ligtermoet Apr 08 '11 at 13:47
  • I asked a question following this one as I'm still stuck on how to target a specific machine using the LocalPrintServer: http://stackoverflow.com/questions/5645892/how-do-i-use-localprintserver-to-target-a-specific-printer – Berry Ligtermoet Apr 13 '11 at 07:38
  • 1
    Note the word *Local*. That targets one very specific machine, the one that the program runs on. – Hans Passant Apr 13 '11 at 11:23
  • Ye sorry, I meant a specific printer. But I've already solved the matter with how to address the printspooler from a specific printer :) See the question for the code fragment. – Berry Ligtermoet Apr 13 '11 at 11:38
  • @BerryLigtermoet I figured that `Server` in `PrintServer` pretty much means the same thing as “daemon”. Just because a process running on your Windows client machine or even simply an API is called a “server” doesn’t mean that process is some other computer offering services, i.e., acting as a “server”. – binki Oct 12 '18 at 14:56
0

you can use WMI to get system information about devices and stuff.

see this article

Bek Raupov
  • 3,782
  • 3
  • 24
  • 42