Interrogating Your Printer Queues with WMI


In response to a newsgroup query here is the code needed to view the printer queues on your system. Once again this is achieved using Windows Management Instrumentation or WMI. Note this code will display all printer queues if there are documents waiting to be printed. To test this open up a printer queue and pause the printer then use notepad or word etc and print to the paused printer.

This code has been written on the release version of .NET

//WMI4.cs
//Demonstrates how to view printjobs for any given printer
//Written 05/17/02 By John O'Donnell - csharpconsulting@hotmail.com

using
System;
using System.Management;
namespace WMI4
{
class Class1
{
static void Main(string[] args)
{
Console.WriteLine("Retrieving printer queue information using WMI");
Console.WriteLine("==================================");
//Query printer queue
System.Management.ObjectQuery oq = new System.Management.ObjectQuery
"SELECT * FROM Win32_PrintJob");
ManagementObjectSearcher query1 =
new ManagementObjectSearcher(oq);
ManagementObjectCollection queryCollection1 = query1.Get();
foreach( ManagementObject mo in queryCollection1 )
{
Console.WriteLine("Printer Driver : " + mo["DriverName"].ToString());
Console.WriteLine("Document Name : " + mo["Document"].ToString());
Console.WriteLine("Document Owner : " + mo["Owner"].ToString());
Console.WriteLine("==================================");
}
}
}
}