【问题标题】:Unable to get the IP Address of network printer?无法获取网络打印机的 IP 地址?
【发布时间】:2014-10-29 03:47:26
【问题描述】:

我需要在我的笔记本电脑上找到已安装打印机的 IP 地址。我在不同的位置和网络之间移动我的笔记本电脑。每个网络都有自己的一组 IP 地址。笔记本电脑的每个位置都安装了不同的打印机,所有连接都是无线进行的。

在使用以下代码 (.net 4.0) 时,QueuePort.Name 返回:

WSD-27e3f972-cdc7-459d-b0c1-20e8410fb1db.0032 和

192.168.1.12_1

由于这些是网络打印机,我假设它们必须解析为真实的 IP 地址??

我哪里错了?或者,还有更好的方法? 非常感谢任何帮助。

 IEnumerable<Printer> GetLocalPrinters()
    {
        EnumeratedPrintQueueTypes[] enumerationFlags = { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections };
        LocalPrintServer printServer = new LocalPrintServer();

        var x = printServer.GetPrintQueues(enumerationFlags).Select(y =>
            new Printer
            {
                Fullname = y.FullName,          
                QueuePortName = y.QueuePort.Name,
                Location = y.Location
            })
            .OrderBy( z => z.QueuePortName);

        return x;

    }

【问题讨论】:

    标签: c# printing .net-4.0 ip-address network-printers


    【解决方案1】:

    端口名不是 IP 地址。有时它们是相同的文本。

    他们的答案似乎在这里: Determine the IP Address of a Printer in C#

    2011 年 10 月 31 日编辑:

    在 WMI 中查询打印机端口 IP 地址。

    using System;
    using System.Management;
    
    namespace WMI_example_01
    {
        class Program
        {
            static void Main(string[] args)
            {
                var scope = new ManagementScope(@"\\.\root\cimv2");
                var query = new ObjectQuery("SELECT * FROM win32_tcpipprinterport");
                var searcher = new ManagementObjectSearcher(scope, query);
                var collection = searcher.Get();
    
                foreach(var col in collection)
                {
                    Console.WriteLine("Port name: {0}\tHostAddress: {1}", col["Name"], col"HostAddress"]);
                }
            }
        }
    }
    

    【讨论】:

    • 阅读。但是,在我的带有佳能打印机的 Windows 7 系统中,Win32_TCPIPPrinterPort 中没有相应的名称条目。那么如何解析 HostName 呢?
    • 对于win32_tcpipprinter端口查询中没有出现的打印机,对应的win32_printer条目是什么?您不能只使用“名称”条目并获取“名称”的 \\ServerName 部分的 IP 地址吗?
    • 不。例如,佳能打印机没有名称条目,直接连接到路由器。它的监视器不包含在注册表中的标准 IP 端口条目中。它完全依赖于第 3 方。
    • 虽然端口未列出,但打印机是否列在 win32_printer 条目中?
    • @VapidLinus 很抱歉,我不是。记忆中,最终的问题是打印机未能始终将其驱动程序放在 Windows 中的“推荐”位置。我最终不得不直接阅读 Windows 注册表。最后,我只是避开了整个事情并使用端口作为窗口识别它们。希望这会有所帮助。
    【解决方案2】:

    打印队列有一个相应的端口,由端口监视器处理。

    有不同的端口监视器(不仅是标准监视器,如 TCPMON 和 WSD,还有自定义和特定于供应商的监视器),据我所知,没有通用的方法来处理所有类型的。

    根据提供的端口名称,我假设您正在处理 WSD 端口。这里事情变得有点棘手,我建议你阅读我的答案https://stackoverflow.com/a/63705944/4700228 以获得解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多