|
-
Feb 13th, 2007, 12:50 PM
#1
Thread Starter
Fanatic Member
[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..."
-
Feb 13th, 2007, 05:24 PM
#2
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
-
Feb 14th, 2007, 11:14 AM
#3
Thread Starter
Fanatic Member
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:
Dim moReturn As Management.ManagementObjectCollection
Dim moSearch As Management.ManagementObjectSearcher
Dim mo As Management.ManagementObject
Try
moSearch = New Management.ManagementObjectSearcher("Select * from Win32_Printer")
moReturn = moSearch.Get
For Each mo In moReturn
MsgBox(mo("name"))
Next
Catch ex As Exception
MsgBox(ex.Message)
Finally
moReturn = Nothing
moSearch = Nothing
mo = Nothing
End Try
Now, if I add
VB Code:
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:
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..."
-
Feb 14th, 2007, 11:35 AM
#4
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....
-
Feb 14th, 2007, 01:00 PM
#5
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:
Private Function GetNetworkPrinterList() As List(Of String)
Dim prtList As New List(Of String)
Dim dirEntry As DirectoryEntry
Dim dirSearcher As DirectorySearcher
Dim resultCollection As SearchResultCollection
Dim result As SearchResult
Try
dirEntry = New DirectoryEntry("")
dirSearcher = New DirectorySearcher(dirEntry)
With dirSearcher
.PageSize = 10
.Filter = "objectCategory=printQueue" ' search filter
.PropertyNamesOnly = True
.PropertiesToLoad.Add("Name")
.SearchScope = SearchScope.Subtree
End With
resultCollection = dirSearcher.FindAll()
For Each result In resultCollection
prtList.Add(result.GetDirectoryEntry.Name.Substring(3))
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return prtList
End Function
Don't forget to add a reference to System.DirectoryServices to your project then import it
VB Code:
Imports System.DirectoryServices
Last edited by stanav; Feb 14th, 2007 at 01:45 PM.
Reason: Add more info
-
Feb 14th, 2007, 01:15 PM
#6
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|