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
  1. //WMI4.cs  
  2. //Demonstrates how to view printjobs for any given printer  
  3. //Written 05/17/02 By John O'Donnell - [email protected]   
  4. using System;  
  5. using System.Management;  
  6. namespace WMI4 {  
  7.  class Class1 {  
  8.   static void Main(string[] args) {  
  9.    Console.WriteLine("Retrieving printer queue information using WMI");  
  10.    Console.WriteLine("==================================");  
  11.    //Query printer queue  
  12.    System.Management.ObjectQuery oq = new System.Management.ObjectQuery "SELECT * FROM Win32_PrintJob");  
  13.   ManagementObjectSearcher query1 = new ManagementObjectSearcher(oq);  
  14.   ManagementObjectCollection queryCollection1 = query1.Get();  
  15.   foreach(ManagementObject mo in queryCollection1) {  
  16.    Console.WriteLine("Printer Driver : " + mo["DriverName"].ToString());  
  17.    Console.WriteLine("Document Name : " + mo["Document"].ToString());  
  18.    Console.WriteLine("Document Owner : " + mo["Owner"].ToString());  
  19.    Console.WriteLine("==================================");  
  20.   }  
  21.  }  


Similar Articles