In the previous blog post we have seen How to print a file/string in C#. In this post we would be looking at how to cancel a print job submitted to the printer. These are some of the common functionalities required in most applications.
For cancellation of the printer job we will be using the System.Management namespace of the .NET framework. Using the ManagementObjectSearcher class we will get a list of all the queued printer jobs. Then using the ManagementObject we will delete the appropriate printer job. Please note that for Cancellation of a printer job, the JobName or JobID is required. Please find below the function that accepts a jobID and then deletes that job from the printer queue. A Boolean true value is returned if successful in deleting the job else a false value is returned.
Remember to Add a Reference to the System.Management .Net assembly.
/// Cancel the print job. This functions accepts the job number.
/// An exception will be thrown if access denied.
/// <param name="printJobID">int: Job number to cancel printing for.</param>
/// <returns>bool: true if cancel successfull, else false.</returns>
public bool CancelPrintJob(int printJobID)
// Variable declarations.
bool isActionPerformed = false;
// Query to get all the queued printer jobs.
searchQuery = "SELECT * FROM Win32_PrintJob";
// Create an object using the above query.
searchPrintJobs = new ManagementObjectSearcher(searchQuery);
// Fire the query to get the collection of the printer jobs.
prntJobCollection = searchPrintJobs.Get();
// Look for the job you want to delete/cancel.
foreach (ManagementObject prntJob in prntJobCollection)
jobName = prntJob.Properties["Name"].Value.ToString();
// Job name would be of the format [Printer name], [Job ID]
splitArr = new char;
splitArr = Convert.ToChar(",");
// Get the job ID.
prntJobID = Convert.ToInt32(jobName.Split(splitArr));
// If the Job Id equals the input job Id, then cancel the job.
if (prntJobID == printJobID)
// Performs a action similar to the cancel
// operation of windows print console
isActionPerformed = true;
catch (Exception sysException)
// Log the exception.