我之前用这种方法得到ID,但是不正确
            LocalPrintServer ps = new LocalPrintServer();
            PrintQueueCollection queue = ps.GetPrintQueues();
            foreach (PrintQueue pq in queue)
            {
                pq.Refresh();
                if (pq.NumberOfJobs > 0)
                {
                    try
                    {
                        PrintJobInfoCollection jobs = pq.GetPrintJobInfoCollection();
                        if (jobs!=null)
                        {
                            foreach (PrintSystemJobInfo job in jobs)
                            {
                                job.Refresh();
                                CancelPrintJob(job.PositionInPrintQueue);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }

解决方案 »

  1.   

    下面是获取打印任务的代码,从队列集合中取出任务名字或者ID,然后调用取消函数
    How to get Printer Submitted Jobs in C# ?#region GetPrintJobsCollection        /// <summary>
            /// Returns the jobs in printer queue
            /// </summary>
            /// <param name="printerName">Printer Name.</param>
            /// <returns>StringCollection</returns>
            public StringCollection GetPrintJobsCollection(string printerName)
            {
                StringCollection printJobCollection = new StringCollection();
                try
                {
                    //Query the printer to get the files waiting to print.
                    string searchQuery = "SELECT * FROM Win32_PrintJob";                ManagementObjectSearcher searchPrintJobs = new ManagementObjectSearcher(searchQuery);
                    ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
                    foreach (ManagementObject prntJob in prntJobCollection)
                    {
                        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 prnName = jobName.Split(splitArr)[0];
                        string documentName = prntJob.Properties["Document"].Value.ToString();
                        if (String.Compare(prnName, printerName, true) == 0)
                        {
                            printJobCollection.Add(documentName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Log the exception.
                }
                return printJobCollection;
            }        #endregion GetPrintJobsCollection
    How to Cancel Printing in C# ?#region CancelPrintJob        /// <summary>
            /// Cancel the print job. This functions accepts the job number.
            /// An exception will be thrown if access denied.
            /// </summary>
            /// <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;
                string searchQuery;
                String jobName;
                char[] splitArr;
                int prntJobID;
                ManagementObjectSearcher searchPrintJobs;
                ManagementObjectCollection prntJobCollection;
                try
                {
                    // 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[1];
                        splitArr[0] = Convert.ToChar(",");
                        // Get the job ID.
                        prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]);
                        // 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
                            prntJob.Delete();
                            isActionPerformed = true;
                            break;
                        }
                    }
                    return isActionPerformed;
                }
                catch (Exception sysException)
                {
                    // Log the exception.
                    return false;
                }
            }        #endregion CancelPrintJob