Common printing questions

UncleRonin

Well-known member
Joined
Feb 28, 2006
Messages
230
Location
South Africa
Programming Experience
5-10
We all have to print things eventually and it can sometimes be quite a bother, depending on the printer and printing method. So I have come up with a bunch of printing related questions that I've wondered about over the yearsr. Some of the answers (like enumerating serial ports) are very straightforward but I figure that if all of the common questions can be put in a single thread it would save folks quite some time searching and also decrease the amount of repeated thread topics. I can only answer a couple of these, the others I have no clue about. I know that there are solutions using Win32, .NET, COM, etc for most of the questions but its always interesting to see the different approaches and compare them. So here are the questions...

How do you enumerate a list of parallel/LPT ports on a PC?

How do you enumerate a list of serial/COM ports on a PC?

How do you enumerate a list of USB ports on a PC?

How do you enumerate a list of network/shared printers?

How do you get a list of installed printers on a PC?

How do you print using a parallel/LPT port?

How do you print using a serial/COM port?

How do you print using a USB port?

How do you print using a network/shared printer?

How do you create a printer programmatically?

How do you share a printer programmatically?

I have a bunch of other questions as well but I think these are more than enough for the meantime. I'll start posting answers when i get some free time so anyone else should add/link theirs so long :p
 
Have you seen the System.Drawing.Printing namespace documentation? PrintDocument is the core class there with other classes being related to configuring this for printing. The InstalledPrinters property (of PrinterSettings of PrintDocument) lists installed printers. You can also set UNC path for PrinterName to print to a network printer not installed locally. (check IsValid to confirm target). With WMI (*) Win32_Printer class you can get information about remote shared printers.

Although not common it is possible to send data directly to port, see these two threads: How to Direct Printing to LPT1 in VB2005 and Printing lines to printer. SerialPort class in System.IO.Ports can list COM ports.

WMI Win32_Share class can be used to create a share.

Not sure if installing a printer programmatically is possible as it requires driver installation. It doesn't matter what port (usb/parallel/serial) a printer is connected to when it is installed, regarding PrintDocument you just select the printer/name.

(*) WMI Code Creator is excellent to try out and get WMI code snippets.
 
Back
Top