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

时间:2011-04-13 07:37:55

标签: c# printing queue

关注此问题:How do I retrieve a list or number of jobs from a printer 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;
}

毕竟这不是那么难!

2 个答案:

答案 0 :(得分:10)

重要提示: 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"    

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

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

是的,你需要自己解析一下。

答案 1 :(得分:4)

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

相关问题