Is there a .NET way to enumerate all available network printers?

后端 未结 6 704
悲&欢浪女
悲&欢浪女 2020-11-28 09:16

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

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 09:44

    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

提交回复
热议问题