views:

441

answers:

5

Is there a straightforward way to enumerate all visible network printers in .NET? Currently, I'm showing the PrintDialog to allow the user to select a printer. The problem with that is, local printers are displayed as well (along with XPS Document Writer and the like). If I can enumerate network printers myself, I can show a custom dialog with just those printers.

Thanks!!

A: 

a good place to start

Fredou
I would prefer to use as little unmanaged code as possible (preferably none). Thanks for the link, though!
Pwninstein
A: 

You can use WMI, here is LinqToWMI Api LINQ to WMI

ArsenMkrt
+1  A: 

PrinterSettiings.InstalledPrinters should give you the collection you want

Robert
PrinterSettings.InstalledPrinters still shows non-network printers, as well as document printers (PDF Writer, XPS Document Writer, etc).
Pwninstein
+4  A: 

found this code here

 private void btnGetPrinters_Click(object sender, EventArgs e)
        {
// 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"])
                    {
                        cmbPrinters.Items.Add(mo[pd.Name]);
                    }
                }
            }

        }

Update:

"This API function can enumerate all network resources, including servers, workstations, printers, shares, remote directories etc."

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=741&lngWId=10

Andrija
+1 Thanks! I can enumerate just the names of installed network printers with a few small adjustments to this code. Now, do you know if one can enumerate all VISIBLE network printers (not just the installed ones) using a similar technique.
Pwninstein
Andrija
A: 

using the new System.Printing API

using (var printServer = new PrintServer(string.Format(@"\\{0}", PrinterServerName)))
{
    foreach (var queue in printServer.GetPrintQueues())
    {
        if (!queue.IsShared)
        {
            continue;
        }
        Debug.WriteLine(queue.Name);
     }
 }
Simon
This only lists local printers, not network printers.
awe
awe: try the updated code
Simon