Results 1 to 6 of 6

Thread: [RESOLVED] Finding all network printers vb.net 2003

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Resolved [RESOLVED] Finding all network printers vb.net 2003

    Hello,
    Sorry, no code. I don't even know where to begin. I have been searching all morning. Like the title implies, I need to get a list of all available network printers, similar (if not identical) to the list displayed when one tries to add a network printer. I just need to get the list, I don't need to add anything. I guess for the moment, all I really need is the \\server\name of the printer. Anyone know how to do this? I know how to find all installed printers for a particular computer, but I need all printers that are on the network.

    Is there a network namespace or something? I can't even figure out how to get a list of all computers on the network. Or is this just going to be really really difficult?

    Thanks
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Finding all network printers vb.net 2003

    Easiest way to do this is to run prnmngr.vbs to list all the printers on a machine/server then redirect the output to your app.
    Type this command in to the command prompt windows and press enter to see what you can get (of course you need to replace servername, domainname, username and password with the correct values, and the user must have admin rights on that server)
    cscript c:\windows\system32\prnmngr.vbs -l -s servername -u domainname\username -w password

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Finding all network printers vb.net 2003

    Using code Stanav posted previously, I was able to retrieve info on all printers on my workstation:
    VB Code:
    1. Dim moReturn As Management.ManagementObjectCollection
    2.         Dim moSearch As Management.ManagementObjectSearcher
    3.         Dim mo As Management.ManagementObject
    4.  
    5.                Try
    6.             moSearch = New Management.ManagementObjectSearcher("Select * from Win32_Printer")
    7.             moReturn = moSearch.Get
    8.             For Each mo In moReturn
    9.                 MsgBox(mo("name"))
    10.             Next
    11.         Catch ex As Exception
    12.             MsgBox(ex.Message)
    13.         Finally
    14.             moReturn = Nothing
    15.             moSearch = Nothing
    16.             mo = Nothing
    17.         End Try

    Now, if I add
    VB Code:
    1. moSearch.Scope.Path.DefaultPath.Server = "adam"
    just before the try/catch, it returns all printers on our "adam" server, and fairly quickly, too.

    If I change that line to read
    VB Code:
    1. moSearch.Scope.Path.DefaultPath.Server = "ws63"
    "ws63" being the name of my workstation, I returns the same info as the unmodified code above, but it takes much much longer.

    So, now, I still don't know how to get a list of all the servers. And even if I did, it could take a very long time to return all the printers from all those machines.

    But, when you try to add a printer in Windows, it creates a list of all printers on the network very quickly. This leads me to believe that when you share a printer, it adds the printer info to some file somewhere. That is the only way I can think that it would be able to gather that info so quickly.

    Is my assumption correct? Does anyone know anything about this?
    Thanks

    P.S. I can also get this info in a command line by using this
    cscript c:\windows\system32\prnmngr.vbs -l -s servername, but it takes about the same amount of time.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  4. #4
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Finding all network printers vb.net 2003

    There is an api call: EnumPrinters

    It's a tricky little beggar to use, but I have code at home....

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Finding all network printers vb.net 2003

    Try this function... Instead of returning a List(Of String), you can modify it to return an arrayList to work with VB.Net 2003
    VB Code:
    1. Private Function GetNetworkPrinterList() As List(Of String)
    2.         Dim prtList As New List(Of String)
    3.         Dim dirEntry As DirectoryEntry
    4.         Dim dirSearcher As DirectorySearcher
    5.         Dim resultCollection As SearchResultCollection
    6.         Dim result As SearchResult
    7.  
    8.         Try
    9.             dirEntry = New DirectoryEntry("")
    10.             dirSearcher = New DirectorySearcher(dirEntry)
    11.             With dirSearcher
    12.                 .PageSize = 10
    13.                 .Filter = "objectCategory=printQueue" ' search filter
    14.                 .PropertyNamesOnly = True
    15.                 .PropertiesToLoad.Add("Name")
    16.                 .SearchScope = SearchScope.Subtree
    17.             End With
    18.             resultCollection = dirSearcher.FindAll()
    19.             For Each result In resultCollection
    20.                 prtList.Add(result.GetDirectoryEntry.Name.Substring(3))
    21.             Next
    22.         Catch ex As Exception
    23.             MsgBox(ex.Message)
    24.         End Try
    25.         Return prtList
    26.     End Function

    Don't forget to add a reference to System.DirectoryServices to your project then import it
    VB Code:
    1. Imports System.DirectoryServices
    Last edited by stanav; Feb 14th, 2007 at 01:45 PM. Reason: Add more info

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Finding all network printers vb.net 2003

    Whoa. I have no idea what this does, yet, but I did get it to work. Its work in an instant and returns the same number of printers as the add printer wizard does. Thank you so much. Now I just gotta figure out how to get more info, and hopefully it is possible to get the info I actually need, which is whether or not it is a bar code printer. However, I'd imagine that info would only be available if the manufacturer actually supplies it. But, thank again everyone who helped.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width