How to get the names of all printers in a network (intranet) in a web app.

How to get the names of all printers in a network (intranet) in a,  web app.

I have used the following code. but It only returns the name of printers installed on the server machine.
But I want all the printers available on a intranet network. which may or  may not be installed on a server machine.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

techprocessAuthor Commented:
I have tried this code. But it only gives the names of the printers installed on server computer. And not on the intranet.
using System.Management;
public void fn(){
// Use the ObjectQuery to get the list of configured printers
        System.Management.ObjectQuery oquery =
            new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
        System.Management.ManagementObjectSearcher mosearcher =
            new System.Management.ManagementObjectSearcher(oquery);
        System.Management.ManagementObjectCollection moc = mosearcher.Get();
        foreach (ManagementObject mo in moc)
            System.Management.PropertyDataCollection pdc = mo.Properties;
            foreach (System.Management.PropertyData pd in pdc)
                if ((bool)mo["Network"])

Open in new window

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

I am not aware of any direct way to query all the printers in the network, specially if these are connected to computer via USB instead of directly connected to network via lan.

But you can do a workaround.

You can write a small app and run it as a service in client machines.
Then you can get the info in either of the 2 ways
1) Query each of these apps from your server when required
2) Let the app send or periodically update the printer details to the server via web service.
Another way, is to install remote printer drivers of all the printers in the intranet.
This might not be possible always based on the network environment.

In this case you can use the above code to get the printer and also do any printing tasks from the server.
Dustin HopkinsConnect With a Mentor Senior Web DeveloperCommented:
I use this in a console application, but it wouldn't take much to port it into a web app
This returns "servername-printername"

    DirectoryServices.DirectoryEntry deParent = new DirectoryServices.DirectoryEntry(); 
    deParent.Username = SvcAcct; // Domain account username
    deParent.Password = SvcPass; // Domain account password
    deParent.AuthenticationType = DirectoryServices.AuthenticationTypes.Secure; 
    DirectoryServices.DirectorySearcher ds = new DirectoryServices.DirectorySearcher(deParent); 
    ds.Filter = "(&(ObjectCategory=printQueue))"; 
    foreach (DirectoryServices.SearchResult result in ds.FindAll()) { 
        Console.WriteLine(result.GetDirectoryEntry().Name); //<-- this is where you would change to response.write/import row into dataset ect.

Open in new window

Dustin HopkinsSenior Web DeveloperCommented:
Oh yeah, forgot to mention, that will only return all printers that are published in active directory/shared.
i am glad it helps. I've ever had a task to list all printer but cancelled. This is what i remember.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.