问题描述
C#如何获取当前打印队列如果打印队列有多个任务,如何获取当前打印的队列我现在只能获取所有的队列stringprintName=bus.GetAttrValueByUserName("选择打印机",Business.userName);PrintServerps=newPrintServer(PrintSystemDesiredAccess.AdministrateServer);PrintQueuepq=newPrintQueue(ps,printName,PrintSystemDesiredAccess.AdministratePrinter);PrintSystemJobInfojobInfo=null;PrintSystemJobInfopreJobInfo=null;while(jobInfo==null){pq.Refresh();varprintJobs=pq.GetPrintJobInfoCollection();foreach(varprintJobinprintJobs){}}
解决方案
解决方案二:
没人吗,自己顶
解决方案三:
c#获取和取消本地打印队列Dictionary<string,int>GetAllPrinterQueues(){RaiseLog("Gettingprintqueuecounts");Dictionary<string,int>TempDict=newDictionary<string,int>();PrintServermyPrintServer=newPrintServer();//GetalltheprintersinstalledonthisPC//Listtheprintserver'squeuesPrintQueueCollectionmyPrintQueues=myPrintServer.GetPrintQueues();StringprintQueueNames="MyPrintQueues:nn";foreach(PrintQueuepqinmyPrintQueues){Saint.StCommon.Wait((decimal)2000);//2secondsof"Application.DoEvents(),notthreadsleepif(GotPingBack(pq.Name)){intPGcount=0;try{if(pq.NumberOfJobs>0){//Weknowtherearejobs.Sowe*have*tobeabletogetthecollectionatsomepointDateTimeBailout=DateTime.Now.AddSeconds(10);//Keeptryingfor10secondsoruntilIgetavalidresponsestringErrMsg="notyetretreived";while(Bailout>DateTime.Now&&ErrMsg!=string.Empty){try{varJobs=pq.GetPrintJobInfoCollection();Saint.StCommon.Wait((int)2);foreach(PrintSystemJobInfoJobinJobs){PGcount+=Job.NumberOfPages;ErrMsg=string.Empty;}}catch(Exceptionk){ErrMsg=k.Message;Console.WriteLine(string.Format("{0}:{1}",pq.Name,k.Message));}}}}catch{Console.WriteLine("Exceptiondork");}Console.WriteLine(string.Format("{2}t{0}t{1}",pq.Name,PGcount,DateTime.Now.ToString("HH:mm:ss.fff")));TempDict.Add(pq.Name,PGcount);}}returnTempDict;}publicboolCancelPrintJob(intprintJobID){//Variabledeclarations.boolisActionPerformed=false;stringsearchQuery;StringjobName;char[]splitArr;intprntJobID;ManagementObjectSearchersearchPrintJobs;ManagementObjectCollectionprntJobCollection;try{//Querytogetallthequeuedprinterjobs.searchQuery="SELECT*FROMWin32_PrintJob";//Createanobjectusingtheabovequery.searchPrintJobs=newManagementObjectSearcher(searchQuery);//Firethequerytogetthecollectionoftheprinterjobs.prntJobCollection=searchPrintJobs.Get();//Lookforthejobyouwanttodelete/cancel.foreach(ManagementObjectprntJobinprntJobCollection){jobName=prntJob.Properties["Name"].Value.ToString();//Jobnamewouldbeoftheformat[Printername],[JobID]splitArr=newchar[1];splitArr[0]=Convert.ToChar(",");//GetthejobID.prntJobID=Convert.ToInt32(jobName.Split(splitArr)[1]);//IftheJobIdequalstheinputjobId,thencancelthejob.if(prntJobID==printJobID){//Performsaactionsimilartothecancel//operationofwindowsprintconsoleprntJob.Delete();isActionPerformed=true;break;}}returnisActionPerformed;}catch(ExceptionsysException){//Logtheexception.returnfalse;}}
解决方案四:
引用2楼hefeng_aspnet的回复:
c#获取和取消本地打印队列Dictionary<string,int>GetAllPrinterQueues(){RaiseLog("Gettingprintqueuecounts");Dictionary<string,int>TempDict=newDictionary<string,int>();PrintServermyPrintServer=newPrintServer();//GetalltheprintersinstalledonthisPC//Listtheprintserver'squeuesPrintQueueCollectionmyPrintQueues=myPrintServer.GetPrintQueues();StringprintQueueNames="MyPrintQueues:nn";foreach(PrintQueuepqinmyPrintQueues){Saint.StCommon.Wait((decimal)2000);//2secondsof"Application.DoEvents(),notthreadsleepif(GotPingBack(pq.Name)){intPGcount=0;try{if(pq.NumberOfJobs>0){//Weknowtherearejobs.Sowe*have*tobeabletogetthecollectionatsomepointDateTimeBailout=DateTime.Now.AddSeconds(10);//Keeptryingfor10secondsoruntilIgetavalidresponsestringErrMsg="notyetretreived";while(Bailout>DateTime.Now&&ErrMsg!=string.Empty){try{varJobs=pq.GetPrintJobInfoCollection();Saint.StCommon.Wait((int)2);foreach(PrintSystemJobInfoJobinJobs){PGcount+=Job.NumberOfPages;ErrMsg=string.Empty;}}catch(Exceptionk){ErrMsg=k.Message;Console.WriteLine(string.Format("{0}:{1}",pq.Name,k.Message));}}}}catch{Console.WriteLine("Exceptiondork");}Console.WriteLine(string.Format("{2}t{0}t{1}",pq.Name,PGcount,DateTime.Now.ToString("HH:mm:ss.fff")));TempDict.Add(pq.Name,PGcount);}}returnTempDict;}publicboolCancelPrintJob(intprintJobID){//Variabledeclarations.boolisActionPerformed=false;stringsearchQuery;StringjobName;char[]splitArr;intprntJobID;ManagementObjectSearchersearchPrintJobs;ManagementObjectCollectionprntJobCollection;try{//Querytogetallthequeuedprinterjobs.searchQuery="SELECT*FROMWin32_PrintJob";//Createanobjectusingtheabovequery.searchPrintJobs=newManagementObjectSearcher(searchQuery);//Firethequerytogetthecollectionoftheprinterjobs.prntJobCollection=searchPrintJobs.Get();//Lookforthejobyouwanttodelete/cancel.foreach(ManagementObjectprntJobinprntJobCollection){jobName=prntJob.Properties["Name"].Value.ToString();//Jobnamewouldbeoftheformat[Printername],[JobID]splitArr=newchar[1];splitArr[0]=Convert.ToChar(",");//GetthejobID.prntJobID=Convert.ToInt32(jobName.Split(splitArr)[1]);//IftheJobIdequalstheinputjobId,thencancelthejob.if(prntJobID==printJobID){//Performsaactionsimilartothecancel//operationofwindowsprintconsoleprntJob.Delete();isActionPerformed=true;break;}}returnisActionPerformed;}catch(ExceptionsysException){//Logtheexception.returnfalse;}}
获取所有队列我能获取到,我想获取的是当前打印的那个队列,请问有办法吗