Search code examples
c#winformsprintingwminetwork-printers

Fetch all printers available in the network - not just local printers


I have been trying to find all printers in the network.But, I always ended up with printers which are already connected with my computer. When I try to add printers at Control Panel\Hardware and Sound\Devices and Printers, I could find many more printers available in the network.

Please note that I don't have printer server. All printers are IP based.

I used following code:

WqlObjectQuery wQuery = new WqlObjectQuery("SELECT * FROM Win32_Printer Where " +
    "Local = FALSE");
ManagementObjectSearcher res = new ManagementObjectSearcher(wQuery);
if ((res.Get().Count > 0))
{
    foreach (ManagementObject printer in res.Get())
    {
        Console.WriteLine(printer["PortName"] + " : " + printer["DriverName"] +
            " : " + printer["Status"]);
    }
}
else { Console.WriteLine("No printers found"); }

In the query, if I set local to true i get printers installed on my computer.

I also tried with similar questions here, but still no luck.

Any help appreciated.


Solution

  • The WMI cannot enumerate network printers, only can list the shared printers registered in the local machine. To this task you can use the WNetEnumResource, WNetOpenEnum and WNetCloseEnum WinApi functions. Some time ago I wrote a sample of this using .Net Try this article Enumerating All Network resources using Delphi Prism, the code uses the Oxygene language but can be translated to C# easily.