【问题标题】:How to find & install a printer that on same WiFi network?如何查找和安装同一 WiFi 网络上的打印机?
【发布时间】:2019-05-13 16:53:34
【问题描述】:

在我的应用程序(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 &amp; scanners)后,我得到了打印机的 ma​​c 地址,然后即使在卸载后我也可以获得它的 IP 地址(建议here

以上都没有帮到我...

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

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

【问题讨论】:

  • 为什么不要求安装打印机?我不知道你为什么要接管这个功能......
  • @RonBeyer - 因为我交付的每个系统都将在谁知道的国家/办事处等。我不知道那里有哪些打印机以及哪些网络......
  • This 将是一个很好的起点。使用 EnumeratedPrintQueueTypes 获取您想要的打印机类型。
  • 感谢@SajithSageer ...但是由于某种原因在我的计算机上运行此代码时(安装了2台打印机和onNote等)-没有结果(printQueuesOnLocalServer.Count = 0)跨度>
  • EnumeratedPrintQueueTypes 给出交集而不是联合。因此,如果您同时添加 EnumeratedPrintQueueTypes.Local、EnumeratedPrintQueueTypes.Shared,它将查找本地和共享的打印机

标签: c# wpf printers network-printers printdialog


【解决方案1】:

另一件事 - 正如@SajithSageer 在上面的 cmets 上建议的那样 (based on this link):

我删除了我的 2 台网络打印机中的 1 台、1 台 HP 和 1 台 Cannon - 两者都在我的 WiFi 网络上找到并从那里安装,结果是:强>
1. 它们都不是“共享的”——它们总是“本地的”
2. 当 Cannon 连接时 - 它也出现在“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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多