Reputation: 2125
I'm creating a small plugin for an application using C# to switch printers automatically based upon the document being printed. Using c# I have a list of printers installed on the system, but need to determine the Spooler ID/Name and the connection type for each printer.
Essentially I am changing a registry key which the host application uses to handle the default printer to use on next print and the value is in the format of Printer Name; Spooler, Connection Type which appear to be unique for different printers.
Here is an example of the value I need to construct: HP LaserJet 5500,winspool,Ne01:
I have the printer name, just not sure how to retrieve the other params for each printer.
The code fragment I am using to retrieve the list of printers relies upon System.Drawing;
foreach (string printer in PrinterSettings.InstalledPrinters)
Console.WriteLine(printer);
var printerSettings = new PrinterSettings();
Console.WriteLine(printerSettings.PrinterName);
}
Upvotes: 0
Views: 634
Reputation: 48129
I Don't know if this helps you out more, or at least down a deeper road to your end-result. I had similar need of looking into printer component / driver settings and have this. Although I have a switch/case for each possible type per the printers I have on my machine there may be others and you can pause and drill into more of the settings which might be deeper nesting elements. One such that I needed was found on queue.DefaultPrintTicket to determine the x/y page resolution for a label printer.
using System.Printing;
var ps = new PrintServer();
var queues = ps.GetPrintQueues(
new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
// The AMSConfig table has width of 70 for binding to match.
StringBuilder sb = new StringBuilder();
foreach (var queue in queues)
{
sb.AppendLine( queue.Name );
if (queue.PropertiesCollection == null)
continue;
foreach (System.Collections.DictionaryEntry ppd in queue.PropertiesCollection)
{
switch( ppd.Value.ToString() )
{
case "System.Printing.IndexedProperties.PrintStringProperty":
var psp = ppd.Value as System.Printing.IndexedProperties.PrintStringProperty;
sb.AppendLine(ppd.Key + " : " + psp.Value);
break;
case "System.Printing.IndexedProperties.PrintInt32Property":
var pip = ppd.Value as System.Printing.IndexedProperties.PrintInt32Property;
sb.AppendLine(ppd.Key + " : " + pip.Value);
break;
case "System.Printing.IndexedProperties.PrintTicketProperty":
var ptp = ppd.Value as System.Printing.IndexedProperties.PrintTicketProperty;
sb.AppendLine(ppd.Key + " : " + ptp.Value);
break;
case "System.Printing.IndexedProperties.PrintBooleanProperty":
var pbp = ppd.Value as System.Printing.IndexedProperties.PrintBooleanProperty;
sb.AppendLine(ppd.Key + " : " + pbp.Value);
break;
case "System.Printing.IndexedProperties.PrintQueueStatusProperty":
var pstap = ppd.Value as System.Printing.IndexedProperties.PrintQueueStatusProperty;
sb.AppendLine(ppd.Key + " : " + pstap.Value);
break;
case "System.Printing.IndexedProperties.PrintQueueAttributeProperty":
var pap = ppd.Value as System.Printing.IndexedProperties.PrintQueueAttributeProperty;
sb.AppendLine(ppd.Key + " : " + pap.Value);
break;
case "System.Printing.IndexedProperties.PrintProcessorProperty":
var ppp = ppd.Value as System.Printing.IndexedProperties.PrintProcessorProperty;
sb.AppendLine(ppd.Key + " : " + ppp.Value);
break;
case "System.Printing.IndexedProperties.PrintPortProperty":
var pportp = ppd.Value as System.Printing.IndexedProperties.PrintPortProperty;
sb.AppendLine(ppd.Key + " : " + pportp.Value);
break;
case "System.Printing.IndexedProperties.PrintServerProperty":
var psvrp = ppd.Value as System.Printing.IndexedProperties.PrintServerProperty;
sb.AppendLine(ppd.Key + " : " + psvrp.Value);
break;
case "System.Printing.IndexedProperties.PrintDriverProperty":
var pdp = ppd.Value as System.Printing.IndexedProperties.PrintDriverProperty;
sb.AppendLine(ppd.Key + " : " + pdp.Value);
break;
}
}
sb.AppendLine("");
sb.AppendLine("");
}
System.IO.File.WriteAllText( "PrinterInfo.txt", sb.ToString());
So at the end of this, I get a top-level list per printer something like...
PaperPort Image Printer
SeparatorFile :
Location :
UntilTimeOfDay : 0
ShareName :
Name : PaperPort Image Printer
Priority : 1
AveragePagesPerMinute : 0
UserPrintTicket :
IsXpsEnabled : False
DefaultPrintTicket :
QueueStatus : None
QueueAttributes : 65
StartTimeOfDay : 0
QueuePrintProcessor : System.Printing.PrintProcessor
QueuePort : System.Printing.PrintPort
DefaultPriority : 1
Comment :
Description : \\[machine]\PaperPort Image Printer,Nuance Image Printer Driver,
HostingPrintServer :
QueueDriver : System.Printing.PrintDriver
NumberOfJobs : 0
Again, some of these objects have values that are other objects to drill deeper, such as getting a COM port too, but again, this should jump you to a bunch more details and hopefully find what you need.
Upvotes: 1