如何在同一WiFi网络上查找和安装打印机?

时间:2019-05-13 16:53:35

标签: c# wpf printers network-printers printdialog

在我的应用程序(win10,WPF)上,我使用户可以打印报告。
但是
当本地计算机上有尚未配置的打印机时,这是不可能的。
(当然,打印机是在网络上激活的,并且通过使用settings -> add printer & sccaners可以找到并安装)

我已经在网上搜索了几天,寻找一种方法来实现:
1.
使用ManagementObjectSearcher("SELECT * from Win32_Printer")
但只安装了 localy 打印选项

2。

尝试了以下代码-但我不知道服务器或打印机的名称
(我可能知道用户在其中运行我的应用的网络名称-但它可以是任何类型的打印机...)

using (ManagementClass win32Printer = new ManagementClass("Win32_Printer"))
{
    using (ManagementBaseObject inputParam = win32Printer.GetMethodParameters("AddPrinterConnection"))
    {
        // Replace <server_name> and <printer_name> with the actual server and
        // printer names.
        inputParam.SetPropertyValue("Name", "\\\\<server_name>\\<printer_name>");

        using (ManagementBaseObject result =
            (ManagementBaseObject)win32Printer.InvokeMethod("AddPrinterConnection", inputParam, null))
        {
            uint errorCode = (uint)result.Properties["returnValue"].Value;

            switch (errorCode)
            {
                case 0:
                    Console.Out.WriteLine("Successfully connected printer.");
                    break;
                case 5:
                    Console.Out.WriteLine("Access Denied.");
                    break;
                case 123:
                    Console.Out.WriteLine("The filename, directory name, or volume label syntax is incorrect.");
                    break;
                case 1801:
                    Console.Out.WriteLine("Invalid Printer Name.");
                    break;
                case 1930:
                    Console.Out.WriteLine("Incompatible Printer Driver.");
                    break;
                case 3019:
                    Console.Out.WriteLine("The specified printer driver was not found on the system and needs to be downloaded.");
                    break;
            }
        }
    }
}
  1. (从Windows add printers & scanners安装打印机驱动器后,我获得了打印机的 mac地址,即使卸载后我也可以获得它的 IP -地址(建议here

以上都不对我有帮助...

因此,我正在寻找以编程方式安装新打印机的任何可能方式,例如:

  1. 使用“添加新打印机”选项启动PrintDialog
  2. 使用windows settings屏幕打开printers & scanners窗口
    发现:Process.Start("ms-settings:printers");
  3. 使用以上任何数据安装新打印机
  4. 以其他任何方式执行....... :)

1 个答案:

答案 0 :(得分:0)

另一件事-@SajithSageer在上述评论(based on this link)中建议:

我删除了我的2台网络打印机中的1台,1台HP和1台Cannon,它们都在我的WiFi网络上找到并从那里安装,结果是:
1.它们都不是“共享的”-它们始终是“本地的”
2.连接大炮时-它也出现在“ EnableBidi”上
3. EnumeratedPrintQueueTypes = 512不存在-但是会给出结果:)

下面是代码:

            for (int i = 0; i < 20; i++)
            {
                EnumeratedPrintQueueTypes[] enumerationFlags = { (EnumeratedPrintQueueTypes)Math.Pow(2,i)};

                LocalPrintServer printServer = new LocalPrintServer();

                //Use the enumerationFlags to filter out unwanted print queues
                PrintQueueCollection printQueuesOnLocalServer = printServer.GetPrintQueues(enumerationFlags);

                Console.WriteLine("These are your shared, local print queues:\t {0}\n---------------------------\n", enumerationFlags[0]);

                foreach (PrintQueue printer in printQueuesOnLocalServer)
                {
                    Console.WriteLine("\t" + printer.Name );
                }
                Console.WriteLine();
            }

结果如下:

These are your shared, local print queues:       Queued
---------------------------


These are your shared, local print queues:       DirectPrinting
---------------------------


These are your shared, local print queues:       4
---------------------------


These are your shared, local print queues:       Shared
---------------------------


These are your shared, local print queues:       Connections
---------------------------


These are your shared, local print queues:       32
---------------------------


These are your shared, local print queues:       Local
---------------------------

        OneNote
        Send To OneNote 2016
        Microsoft XPS Document Writer
        Microsoft Print to PDF
        HP872916 (HP OfficeJet Pro 7740 series)
        Fax - HP OfficeJet Pro 7740 series
        Fax

These are your shared, local print queues:       EnableDevQuery
---------------------------


These are your shared, local print queues:       KeepPrintedJobs
---------------------------


These are your shared, local print queues:       512
---------------------------

        Send To OneNote 2016
        Microsoft XPS Document Writer
        Microsoft Print to PDF
        HP872916 (HP OfficeJet Pro 7740 series)

These are your shared, local print queues:       WorkOffline
---------------------------


These are your shared, local print queues:       EnableBidi
---------------------------


These are your shared, local print queues:       RawOnly
---------------------------


These are your shared, local print queues:       PublishedInDirectoryServices
---------------------------


These are your shared, local print queues:       Fax
---------------------------

        Fax

These are your shared, local print queues:       TerminalServer
---------------------------


These are your shared, local print queues:       65536
---------------------------


These are your shared, local print queues:       PushedUserConnection
---------------------------


These are your shared, local print queues:       PushedMachineConnection
---------------------------


These are your shared, local print queues:       524288
---------------------------


Press enter to continue.