Jaa


Looking at the Installed Printers

Question: I am writing a Windows forms application using Visual Studio 2003. One of the requirements for the application is that I need to retrieve a list of printers installed on the local machine. How can I do that? I suppose I would need to call an API but I am just not sure?

 

Answer: The System.Drawing.Printer.PrinterSettings class can be used to retrieve printer information. This class contains information about the installed printers and their settings information. Within this a list of installed printers can be obtained by enumerating the InstalledPrinters collection which includes every printer that’s installed on the local computer.

 

For example, the following code can be used to retrieve the list of installed printers on my local machine.

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Printer As New System.Drawing.Printing.PrinterSettings

        Dim PrinterName As String

        For Each printername In System.Drawing.Printing.PrinterSettings.InstalledPrinters

            MsgBox("Printer:" & PrinterName)

  Next

End Sub

 

If you want to find more information about the settings for a specific printer, you can create a PrinterSettings instance and set the PrinterName property with a valid name. If for example we wanted to extend the code above to list the possible paper sources for a printer.

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Printer As New System.Drawing.Printing.PrinterSettings

        Dim PrinterName As String

        For Each printername In System.Drawing.Printing.PrinterSettings.InstalledPrinters

            MsgBox("Printer:" & PrinterName)

            Printer.PrinterName = PrinterName

            If Printer.IsValid Then

                ' to retrieve the possible paper sources for this printer

                Dim PSource As System.Drawing.Printing.PaperSource

                For Each PSource In Printer.PaperSources

                    MsgBox(PSource.ToString)

                Next

            End If

        Next

End Sub