2011-04-13 110 views
3

下面这个问题:How do I retrieve a list or number of jobs from a printer queue?如何使用LocalPrintServer定位特定的打印机?

我仍然坚持如何针对特定的打印机,我目前只知道使用LocalPrintServer类的名称。该应用程序应该一次打印到多台机器,所有打印机打印机需要分别进行监控。任何人都可以提供一段代码片断,说明我如何仅使用打印机的名称实例化LocalPrintServer对象?

在此先感谢!

编辑:添加的解决方案的代码片段:

private int GetNumberOfPrintJobs() 
{ 
    LocalPrintServer server = new LocalPrintServer(); 
    PrintQueueCollection queueCollection = server.GetPrintQueues(); 
    PrintQueue printQueue = null; 

    foreach (PrintQueue pq in queueCollection) 
    { 
     if (pq.FullName == PrinterName) //PrinterName is a classmember 
      printQueue = pq; 
    } 

    int numberOfJobs = 0; 
    if (printQueue != null) 
     numberOfJobs = printQueue.NumberOfJobs; 

    return numberOfJobs; 
} 

这毕竟不是那么难!

回答

4

尝试指定打印机名称的LocalPrintServer.GetPrintQueue。

+0

这实际上是我想要尝试的。我会检查出来的! – 2011-04-13 08:30:16

9

重要提示:GetPrintQueues不返回从用户的角度安装的所有打印机 - 只是那些由本地服务器“拥有”。

更奇怪的是,LocalPrintServer.DefaultPrintQueue不一定包含在GetPrintQueues()内,即使它来自LocalPrintServer对象。

如果您使用的是System.Drawing.Printing.PrinterSettings.InstalledPrinters这是一个string[],您会从用户的角度看到所有安装的打印机列表。

如果您安装了远程打印机(在打印服务器上),其中一些可能位于远程机器上。如果是通过IP访问网络打印机,然后它仍然是一个本地打印机:

"Send To OneNote 2010" 
"Microsoft XPS Document Writer" 
"HP LaserJet P2050 Series PCL6" 
"HP LaserJet 1020" 
"Fax" 
"\\\\ike\\LUCY" 
"\\\\shipping\\HP LaserJet 1020"  

到远程服务器上检索打印队列,你需要做的:

new PrintServer("\\ike").GetPrintQueue("LUCY") 

是你需要解析它自己。

+4

您也可以通过将EnumeratedPrintQueueTypes数组传递给GetPrintQueues()来获得网络打印机,请参阅:http://stackoverflow.com/questions/6763374/c-batch-plot-application-printserver-printqueue-issues/6771934# 6771934 – salle55 2011-10-05 17:34:44

+0

@ salle55更容易 - 谢谢:-) – 2011-10-08 11:36:24