0

I want to get all the printers installed or connected to the PC in dropdownlist.

I've done the following code for that.

Public Function FillddlPrinters() As Boolean
        Dim printersettings As New System.Drawing.Printing.PrinterSettings
        Dim Cnt As Integer = 0

        Me.ddlPrinter.Items.Clear()

        For i = 0 To Drawing.Printing.PrinterSettings.InstalledPrinters.Count - 1

            If Drawing.Printing.PrinterSettings.InstalledPrinters(i).ToString.ToUpper.Trim.Contains("XPS") Or _
                  Drawing.Printing.PrinterSettings.InstalledPrinters(i).ToString.ToUpper.Trim.Contains("FAX") Or _
                  Drawing.Printing.PrinterSettings.InstalledPrinters(i).ToString.ToUpper.Trim.Contains("PDF") Then

                Continue For

            End If

            Me.ddlPrinter.Items.Insert(Cnt, Drawing.Printing.PrinterSettings.InstalledPrinters(i))
            Cnt += 1
        Next

        Me.ddlPrinter.Items.Insert(0, "Select Printer")

    End Function

But it's not giving me all the printers. I'm not getting where it is going wrong.

The printer which is not displaying is on the network. And I'm successfully connected to the network. But though it is not getting displayed.

Meanwhile it is working ok on other machine in different network. Than why not on mine??

Please help me out with this.

I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216

1 Answers1

0

Your code will only show the printers that are installed locally. If you want to find printers on the network you can use a WMI query.

Add a reference to the System.Management namespace, then use this code:

    ' Use the ObjectQuery to get the list of configured printers
    Dim oquery As System.Management.ObjectQuery = New System.Management.ObjectQuery("SELECT * FROM Win32_Printer")

    Dim mosearcher As System.Management.ManagementObjectSearcher = New System.Management.ManagementObjectSearcher(oquery)

    Dim moc As System.Management.ManagementObjectCollection = mosearcher.Get()

    For Each mo As System.Management.ManagementObject In moc
        Debug.WriteLine(mo.ToString)
    Next

Modified from this source: http://www.dotnetcurry.com/showarticle.aspx?ID=148

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143