How to enumerate all the printers on the network

F

Frank Rizzo

I basically need a list of printers that's returned by the Find Printers
dialog ( http://www.sqleffects.com/mystuff/findPrinters.png ). I've
tried the path of

DirectoryEntry entry = new DirectoryEntry(strPath);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(objectCategory=printer)"
foreach(SearchResult result in mySearcher.FindAll())
{
strName = result.GetDirectoryEntry().Name;
}

but nothing was returned.

I am wondering what mechanism the Find Printers dialog is using? Is it
hitting up the active directory? How does it find all the printers?

Thanks.
 
A

Arne Vajhøj

Peter said:
Printers aren't really in a directory per se. They are simply installed
drivers. You can use (via p/invoke) the unmanaged EnumPrinters()
function to find all the installed printers. It's possible WMI in .NET
has an equivalent method, but I don't know it off the top of my head.

It can.

using System;
using System.Management;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
WqlObjectQuery q = new WqlObjectQuery("SELECT * FROM
Win32_Printer");
ManagementObjectSearcher res = new ManagementObjectSearcher(q);
foreach (ManagementObject p in res.Get())
{
Console.WriteLine(p["PortName"] + " : " +
p["DriverName"] + " : " +p["Status"]);
}
}
}
}

Arne
 
F

Frank Rizzo

Arne,

This code only brings back locally installed printers. I want the
printers that are returned by the Find Printers dialog, which somehow
seems to find every single printer in the enterprise (hundreds).


Peter said:
Printers aren't really in a directory per se. They are simply
installed drivers. You can use (via p/invoke) the unmanaged
EnumPrinters() function to find all the installed printers. It's
possible WMI in .NET has an equivalent method, but I don't know it off
the top of my head.

It can.

using System;
using System.Management;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
WqlObjectQuery q = new WqlObjectQuery("SELECT * FROM
Win32_Printer");
ManagementObjectSearcher res = new ManagementObjectSearcher(q);
foreach (ManagementObject p in res.Get())
{
Console.WriteLine(p["PortName"] + " : " +
p["DriverName"] + " : " +p["Status"]);
}
}
}
}

Arne
 
A

Arne Vajhøj

Frank said:
Arne said:
Peter said:
I am wondering what mechanism the Find Printers dialog is using? Is
it hitting up the active directory? How does it find all the printers?

Printers aren't really in a directory per se. They are simply
installed drivers. You can use (via p/invoke) the unmanaged
EnumPrinters() function to find all the installed printers. It's
possible WMI in .NET has an equivalent method, but I don't know it
off the top of my head.

It can.

using System;
using System.Management;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
WqlObjectQuery q = new WqlObjectQuery("SELECT * FROM
Win32_Printer");
ManagementObjectSearcher res = new
ManagementObjectSearcher(q);
foreach (ManagementObject p in res.Get())
{
Console.WriteLine(p["PortName"] + " : " +
p["DriverName"] + " : " +p["Status"]);
}
}
}
}
This code only brings back locally installed printers. I want the
printers that are returned by the Find Printers dialog, which somehow
seems to find every single printer in the enterprise (hundreds).

You can get WMI to return all printers available at servers via
ManagementScope.

But maybe LDAP is more what you want.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top