Click here to Skip to main content
Click here to Skip to main content

A simple approach for controlling print jobs using WMI

, 1 Apr 2004
Rate this:
Please Sign up or sign in to vote.
A simple approach for controlling print jobs using WMI.

Introduction

This article presents a simplified approach of controlling print jobs using WMI. To know more about WMI, please visit MSDN.

Why WMI? Because it provides a simplified approach and avoids making API calls within our C# code. WMI comes as default for Windows XP and Windows 2000. For Windows 95/98, we need to download WMI Core 1.5 and install it: WMI Core 1.5

The Approach

First, let's see how to get the list of printers.

public static StringCollection GetPrintersCollection()
{
    StringCollection printerNameCollection = new StringCollection();
    string searchQuery = "SELECT * FROM Win32_Printer";
    ManagementObjectSearcher searchPrinters = 
          new ManagementObjectSearcher(searchQuery);
    ManagementObjectCollection printerCollection = searchPrinters.Get();
    foreach(ManagementObject printer in printerCollection)
    {
        printerNameCollection.Add(printer.Properties["Name"].Value.ToString());
    }
    return printerNameCollection;
}

The above method returns the list of printers configured in the local machine. The printer ManagementObject exposes many useful properties. Using the PaperSizesSupported property, we can get the list of paper sizes supported by that particular printer. To view information about Win32_Printer, please refer MSDN.

Using this printer name, we can fetch the print job collection by using the following method:

public static StringCollection GetPrintJobsCollection(string printerName)
{
  StringCollection printJobCollection = new StringCollection();
  string searchQuery = "SELECT * FROM Win32_PrintJob";
  
  /*searchQuery can also be mentioned with where Attribute,
      but this is not working in Windows 2000 / ME / 98 machines 
      and throws Invalid query error*/
  ManagementObjectSearcher searchPrintJobs = 
            new ManagementObjectSearcher(searchQuery);
  ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
  foreach(ManagementObject prntJob in prntJobCollection)
  {
    System.String jobName = prntJob.Properties["Name"].Value.ToString();

    //Job name would be of the format [Printer name], [Job ID]
    char[] splitArr = new char[1];
    splitArr[0] = Convert.ToChar(",");
    string prnterName = jobName.Split(splitArr)[0];
    string documentName = prntJob.Properties["Document"].Value.ToString();
    if(String.Compare(prnterName, printerName, true) == 0)
    {
      printJobCollection.Add(documentName);
    }
  }
  return printJobCollection;
}

The query "SELECT * FROM Win32_PrintJob" can also be used as "SELECT * FROM Win32_PrintJob WHERE Name like '"+ printerName.Replace("\", "\\") +"%'". But the query with WHICH attribute caused problems in my system which was running Windows 2000 but ran smooth in Windows XP machines. So currently, I am making a loop through all the print jobs and identify print jobs for that particular printer.

Now, let's see how to manage these print jobs. The print console provided by Windows allows us to Pause, Resume & Cancel print job. It also allows to set priority for a print job. Using WMI, we can perform Pause, Resume & Cancel, but it doesn't provide any method for changing the priority level.

The following code depicts how to pause a print job:

public static bool PausePrintJob(string printerName, int printJobID)
{
  bool isActionPerformed = false;
  string searchQuery = "SELECT * FROM Win32_PrintJob";
  ManagementObjectSearcher searchPrintJobs = 
           new ManagementObjectSearcher(searchQuery);
  ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
  foreach(ManagementObject prntJob in prntJobCollection)
  {
    System.String jobName = prntJob.Properties["Name"].Value.ToString();
    //Job name would be of the format [Printer name], [Job ID]
    char[] splitArr = new char[1];
    splitArr[0] = Convert.ToChar(",");
    string prnterName = jobName.Split(splitArr)[0];
    int prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]);
    string documentName = prntJob.Properties["Document"].Value.ToString();
    if(String.Compare(prnterName, printerName, true) == 0)
    {
      if(prntJobID == printJobID)
      {
        prntJob.InvokeMethod("Pause", null);
        isActionPerformed = true; 
        break;
      }
    }
  }
  return isActionPerformed;
}

By invoking the Pause method, we can pause the print job. Similar approach is required for resuming the print job. Just we need to invoke the Resume method.

prntJob.InvokeMethod("Resume", null);

Win32_PrintJob WMI_CLASS provides methods to pause and resume a print job. But it doesn't provide any method for canceling the print job. To cancel the print job, you need to just delete the management object.

public static bool CancelPrintJob(string printerName, int printJobID)
{
  bool isActionPerformed = false;
  string searchQuery = "SELECT * FROM Win32_PrintJob";
  ManagementObjectSearcher searchPrintJobs = 
         new ManagementObjectSearcher(searchQuery);
  ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
  foreach(ManagementObject prntJob in prntJobCollection)
  {
    System.String jobName = prntJob.Properties["Name"].Value.ToString();
    //Job name would be of the format [Printer name], [Job ID]
    char[] splitArr = new char[1];
    splitArr[0] = Convert.ToChar(",");
    string prnterName = jobName.Split(splitArr)[0];
    int prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]);
    string documentName = prntJob.Properties["Document"].Value.ToString();
    if(String.Compare(prnterName, printerName, true) == 0)
    {
      if(prntJobID == printJobID)
      {
        //performs a action similar to the cancel 
        //operation of windows print console
        prntJob.Delete();
        isActionPerformed = true; 
        break;
      }
    }
  }
  return isActionPerformed;
}

That's it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jagan Mohan
Web Developer
India India
My favourite quote "Temet Nosce" which means "Know Thyself". And I am working on that.

Comments and Discussions

 
GeneralWatching for Print jobs Pinmemberrwgt0su28-Jan-10 10:18 
GeneralRe: Watching for Print jobs PinmemberApurva36338-Aug-11 3:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140718.1 | Last Updated 2 Apr 2004
Article Copyright 2004 by Jagan Mohan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid