有没有一个.NET的方式来枚举所有可用的网络打印机?打印机、方式、网络、NET

2023-09-02 11:47:32 作者:残月青衣踏尘吟

有没有一种简单的方法来枚举在.NET中所有可见的网络打印机?目前,我展示PrintDialog类,以允许用户选择一个打印机。这问题是,本地打印机的显示,以及(连同XPS文档写入等等)。如果我可以枚举网络打印机我自己,我可以证明只有那些打印机的自定义对话框。

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.

谢谢!

推荐答案

发现这个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]);
                    }
                }
            }

        }

更新:

这个API函数可以枚举所有的网络资源,包括服务器,工作站,打印机,共享,远程目录等。

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

http://www.planet-source-$c$c.com/vb/scripts/Show$c$c.asp?txt$c$cId=741&lngWId=10