4

When I go to Printers and Faxes dialog, I can click the Add a printer link, select Network Printer, then Find a printer in the directory. From there I get a dialog box which lets me find ALL printers in the enterprise.

I need to find all the network printers with my code. How can I do this?

Note that I am not talking about network printers that are connected to my PC, but all network printers in the enterprise (my workplace has almost 4000 printers).

P.S. PrintServer().GetPrintQueues only returns printers attached to the computer.

P.P.S. Here is a short video of what I want: http://www.angryhacker.com/toys/FindAllPrinters/FindAllPrinters.html

AngryHacker
  • 59,598
  • 102
  • 325
  • 594

1 Answers1

4

DirectorySearcher with a filter for (objectClass=printer) (objectClass=printQueue)should do the trick.

using (var e = new DirectoryEntry("LDAP://DC=example,DC=com"))
    using (var s = new DirectorySearcher(e)) {
        s.Filter = "(objectClass=printQueue)";

        using (var c = s.FindAll()) {
            WL("Returned {0} objects", c.Count);
            foreach (SearchResult r in c) {
                WL("{0}", r.Path);
            }
        }
    }
Mark Brackett
  • 84,552
  • 17
  • 108
  • 152
  • Nope, it returns nothing with the filter. Without the filter, it just returns gobs of exchange email groups, but no printers at all. – AngryHacker Apr 02 '10 at 16:54
  • Yes, I found the same thing. Thanks for your help. – AngryHacker Apr 03 '10 at 04:36
  • If this didn't solve your issue why did you accept it as the answer? It is confusing for people looking into this later and could prevent people who could help from doing so. – Brad May 21 '18 at 12:27
  • 1
    @Brad - you have to read comments, edit and accepted (from hovering on checkbox) in timestamp order; I originally had listed objectClass=printer which doesn't exist; switching to objectClass=printQueue and adding example code got the list he needed (at which point it was accepted). – Mark Brackett May 24 '18 at 12:11