How do I use LocalPrintServer to target a specific printer?

心已入冬 提交于 2020-01-01 06:06:51

问题


Following this question: How do I retrieve a list or number of jobs from a printer queue?

I'm still stuck on how to target a specific printer of which I currently only know the name using the LocalPrintServer class. The application is supposed to print to several machines at once and all printspoolers need to be monitored separately. Can anyone provide me with a code snippet that shows how I can instantiate a LocalPrintServer object using only the name of the printer?

Thanks in advance!

Edit: Added code fragment of solution:

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

That wasn't so hard after all!


回答1:


Try the LocalPrintServer.GetPrintQueue specifying the printer name.




回答2:


Important Note: GetPrintQueues doesn't return all printers installed from the user's perspective - just those that are 'owned' by the local server.

More oddly though, LocalPrintServer.DefaultPrintQueue isn't necessarily contained within GetPrintQueues() even though it comes from the LocalPrintServer object.

If you use System.Drawing.Printing.PrinterSettings.InstalledPrinters which is a string[] you'll get a list of all printers installed from the user's perspective.

Some of these may be on remote machines if you've installed a remote printer (on a print server). If it's a networked printer accessible by IP then it will still be a local printer:

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

To retrieve a printqueue on a remote server you need to do:

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

Yes you'll need to parse it out yourself.



来源:https://stackoverflow.com/questions/5645892/how-do-i-use-localprintserver-to-target-a-specific-printer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!