How do I get a list of all the printers on a network?

I have an odd customer requirement that they can be able to print from a desktop app to any printer on their network in any of their domains without a driver. I have this working; the problem that I am having is in retrieving a list of all the printers on the network.   I have tried the API functions and I tried this code also.

 Dim moReturn As Management.ManagementObjectCollection

        Dim moSearch As Management.ManagementObjectSearcher

        Dim mo As Management.ManagementObject

        moSearch = New Management.ManagementObjectSearcher("Select * from Win32_Printer")

        moReturn = moSearch.Get
        Dim i = moSearch.Get.Count
        Dim s = moSearch.ToString()
       
        For Each mo In moReturn

            Dim objReturn As Object
            Dim test = mo("Name").ToString()
            ComboBox1.Items.Add(test)

        Next

But I only get the printers registered to my machine.

Any help would be appreciated thank you.
Chris333Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DotNetLover_BaanCommented:
Hi there.. here is the VB code. I am populating a combo box with the available printers in the network.

        Dim strprinters As String
        ComboBox1.Items.Clear()
        For Each strprinters In System.Drawing.Printing.PrinterSettings.InstalledPrinters
            ComboBox1.Items.Add(strprinters)
        Next

-Baan
Bob LearnedCommented:
Do you have 1 machine that acts at the print server for the network?

If you want to use WMI to access remote machines, then do something like this:

' Add the 'System.Management' reference to the project from the .NET tab.
Imports System.Management

Public Class Win32_Printer

  Public Shared Function GetPrinterList(ByVal machineName As String, ByVal userName As String, ByVal userPassword As String) As String()

    Dim options As New ConnectionOptions
    options.Username = userName
    options.Password = userPassword

    Dim path As New ManagementPath
    path.Server = machineName

    Dim scope As New ManagementScope(path)
    scope.Options = options

    Dim searcher As New ManagementObjectSearcher("Select * from Win32_Printer")
    searcher.Scope = scope

    Dim list As New ArrayList

    For Each obj As ManagementObject In searcher.Get()
      list.Add(obj("Name").ToString())
    Next obj

    Return list.ToArray(GetType(String))

  End Function  'GetPrinterList

End Class


Sample usage:
   Dim listPrinters() As String = Win32_Printer.GetPrinterList("computer", "user", "password")

Bob
Bob LearnedCommented:
Here is a new and improved version that can check either local machine or a remote machine (using overloaded methods):

' Add the 'System.Management' reference to the project from the .NET tab.
Imports System.Management

Public Class Win32_Printer

  Public Overloads Shared Function GetPrinterList() As String()

    ' Local machine.
    Return BuildPrinterList(Nothing)

  End Function  'GetPrinterList

  Public Overloads Shared Function GetPrinterList(ByVal machineName As String, ByVal userName As String, ByVal userPassword As String) As String()

    Dim options As New ConnectionOptions
    options.Username = userName
    options.Password = userPassword

    Dim path As New ManagementPath
    path.Server = machineName

    Dim scope As New ManagementScope(path)
    scope.Options = options

    ' Remote machine
    Return BuildPrinterList(scope)

  End Function  'GetPrinterList


  Private Shared Function BuildPrinterList(ByVal scope As ManagementScope)

    Dim searcher As New ManagementObjectSearcher("Select * from Win32_Printer")

    If Not scope Is Nothing Then
      searcher.Scope = scope
    End If

    Dim list As New ArrayList

    For Each obj As ManagementObject In searcher.Get()
      list.Add(obj("Name").ToString())
    Next obj

    Return list.ToArray(GetType(String))

  End Function  'BuildPrinterList

End Class

Sample usage:
   ' Remote machine
   Dim listPrinters() As String = Win32_Printer.GetPrinterList("computer", "user", "password")
   ' Local machine
   Dim listPrinters() As String = Win32_Printer.GetPrinterList()

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris333Author Commented:
Thank you for the solution it works great.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.