c# 获取和取消本地打印队列

Dictionary<stringint> GetAllPrinterQueues()
{
    RaiseLog(
"Getting print queue counts");
    Dictionary
<stringint> TempDict = new Dictionary<stringint>();

    PrintServer myPrintServer 
= new PrintServer(); // Get all the printers installed on this PC

    
// List the print server's queues
    PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
    String printQueueNames 
= "My Print Queues:\n\n";
    
foreach (PrintQueue pq in myPrintQueues)
    {
        Saint.StCommon.Wait((
decimal)2000); // 2 seconds of "Application.DoEvents(), not thread sleep
        if (GotPingBack(pq.Name))
        {
            
int PGcount = 0;
            
try
            {
                
if (pq.NumberOfJobs > 0)
                {
                    
// We know there are jobs.  So we *have* to be able to get the collection at some point
                    DateTime Bailout = DateTime.Now.AddSeconds(10); // Keep trying for 10 seconds or until I get a valid response
                    string ErrMsg = "notyetretreived";
                    
while (Bailout > DateTime.Now && ErrMsg != string.Empty)
                    {
                        
try
                        {
                            
var Jobs = pq.GetPrintJobInfoCollection();
                            Saint.StCommon.Wait((
int)2);
                            
foreach (PrintSystemJobInfo Job in Jobs)
                            {
                                PGcount 
+= Job.NumberOfPages;
                                ErrMsg 
= string.Empty;
                            }
                        }
                        
catch (Exception k)
                        {
                            ErrMsg 
= k.Message;
                            Console.WriteLine(
string.Format("{0}: {1}", pq.Name, k.Message));
                        }
                    }
                }
            }
            
catch
            {
                Console.WriteLine(
"Exception dork");
            }
            Console.WriteLine(
string.Format("{2}\t{0}\t{1}", pq.Name, PGcount, DateTime.Now.ToString("HH:mm:ss.fff")));
            TempDict.Add(pq.Name, PGcount);
        }
    }
    
return TempDict;
}


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;            
    }        
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值