C#打印队列ID问题

飞枭雷影 2014-06-06 11:19:31
CSDN上找到的源代码,其中 printJobId是从哪里来的?能否先列出打印队列的文档名,在根据文档名得到ID,在根据ID确定是否要删除,希望高手给点指点,只要正确马上给分。
public bool CancelPrintJob(int printJobId)
{
bool isActionPerformed = false;
string searchQuery;
String JobName;
char[] splitArr;
int prntJobID;
ManagementObjectSearcher searchPrintJobs;
ManagementObjectCollection prntJobCollection;
try
{
searchQuery = "SELECT * FROM Win32_PrintJob";
searchPrintJobs = new ManagementObjectSearcher(searchQuery);
prntJobCollection = searchPrintJobs.Get();

foreach(ManagementObject prntJob in prntJobCollection)
{
JobName = prntJob.Properties["NAME"].Value.ToString();
splitArr = new char[1];
splitArr[0] = Convert.ToChar(",");
prntJobID = Convert.ToInt32(JobName.Split(splitArr)[1]);
if (prntJobID == printJobId)
{
prntJob.Delete();
isActionPerformed = true;
break;
}
}
return isActionPerformed;
}
catch (Exception ex)
{
return false;
}
}
...全文
178 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
save4me 2014-06-07
  • 打赏
  • 举报
回复
下面是获取打印任务的代码,从队列集合中取出任务名字或者ID,然后调用取消函数 How to get Printer Submitted Jobs in C# ?
引用
Here is a handy method that gives you a collection of all the jobs submitted to the printer. This method uses the ManagementObjectSearcher and the ManagementObjectCollection classes from the .NET framework. The query “SELECT * FROM Win32_PrintJob” is used to get a collection of the submitted jobs. These jobs are added to a StringCollection and this collection is returned back.

#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# ?
引用
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.

#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
飞枭雷影 2014-06-06
  • 打赏
  • 举报
回复
我之前用这种方法得到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) { } } }

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧