如何使用LocalPrintServer定位特定打印机?

Ber*_*oet 4 c# printing queue

以下问题:如何从打印机队列中检索列表或作业数?

我仍然坚持如何定位一个特定的打印机,我目前只使用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;
}
Run Code Online (Sandbox Code Playgroud)

毕竟这不是那么难!

Sim*_*ver 10

重要说明: GetPrintQueues不会从用户的角度返回所有安装的打印机 - 只是本地服务器"拥有"的打印机.

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

如果您使用的System.Drawing.Printing.PrinterSettings.InstalledPrintersstring[]从用户的角度来看,您将获得所有打印机的列表.

如果您安装了远程打印机(在打印服务器上),其中一些可能在远程计算机上.如果它是可通过IP访问的联网打印机,那么它仍然是本地打印机:

"Send To OneNote 2010"  
"Microsoft XPS Document Writer" 
"HP LaserJet P2050 Series PCL6" 
"HP LaserJet 1020"  
"Fax"   
"\\\\ike\\LUCY" 
"\\\\shipping\\HP LaserJet 1020"    
Run Code Online (Sandbox Code Playgroud)

要在远程服务器上检索printqueue,您需要执行以下操作:

new PrintServer("\\ike").GetPrintQueue("LUCY")
Run Code Online (Sandbox Code Playgroud)

是的,你需要自己解析它.

  • 您也可以通过将EnumeratedPrintQueueTypes数组传递给GetPrintQueues()来获取网络打印机,请参阅:http://stackoverflow.com/questions/6763374/c-batch-plot-application-printserver-printqueue-issues/6771934#6771934 (5认同)

Mub*_*han 5

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