totoro
totoro

Reputation: 3407

how to get filepaths of print jobs in queue in C#

I would like to get the list of queued print jobs and their filepaths using C# in .NET 4. I looked at this, but didn't see anything regarding filepath. I tried the following code (modified from an example somewhere), hoping job.Name or job.JobName is what i am looking for, but it doesn't run:

calling GetPrintJobInfoCollection() always raises NullReferenceException when there is a job (printer is offline, so the job is always waiting in queue). The exception does not occur when jobs are deleted from queue (by clicking on the printer icon in lower right corner).

so 2 questions:

  1. why NullReferenceException?
  2. Any other ways to get queued filepaths if this method fails?

Thanks in advance!

public static void testPrinter()
{
    PrintQueueCollection printQueues = null;
    PrintServer printServer = new PrintServer();
    printQueues = printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });

    foreach (PrintQueue printQueue in printQueues)
    {
        foreach (var job in printQueue.GetPrintJobInfoCollection())
        {
            Console.WriteLine(string.Format("jobname={0} name={1} size={2} status={3}", job.JobName, job.Name, job.JobSize, job.JobStatus));
        }
    }
}

Upvotes: 2

Views: 1344

Answers (1)

totoro
totoro

Reputation: 3407

As it turns out, the exception only occurs when the printer is offline. When the printer is online, job.Name gives the filepath, while job.JobName only says "Print System Document".

Upvotes: 1

Related Questions