0

I need to find the IP address of the installed printers on my laptop. I move my laptop between different locations and networks. Each network has its own set of ip addresses. The laptop has different printers installed for each location with all connections being made wirelessly.

In using the below code (.net 4.0), the QueuePort.Name returns:

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

192.168.1.12_1

Since these are network printers, I assume these have to resolve to a real IP Address??

Where am I going wrong? Or is there a better way? Any help is really appreciated.

 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;

    }
Alan Wayne
  • 5,122
  • 10
  • 52
  • 95

2 Answers2

2

The portname is NOT the IP address. Sometimes they are the same text.

They answer appears to be here: Determine the IP Address of a Printer in C#

Edited 31-Oct-2011:

Query the WMI for the printer port IP address.

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"]);
            }
        }
    }
}
Community
  • 1
  • 1
m_a_s
  • 240
  • 2
  • 6
  • 1
    Read it. However, in my windows 7 system with a Canon printer, there is no corresponding Name entry in Win32_TCPIPPrinterPort. So how does one resolve the HostName? – Alan Wayne Oct 29 '14 at 14:53
  • For the printers that do not appear in the win32_tcpipprinter port query, what are the corresponding win32_printer entries? Can't you just use the "Name" entry and get the IP address for the \\ServerName portion of the "Name"? – m_a_s Nov 01 '14 at 01:51
  • nope. As an example, the canon printer has no name entry and is directly connected to the router. It's monitor is not included in the standard ip port entries in the registery. It is completely 3rd party dependent. – Alan Wayne Nov 01 '14 at 04:39
  • While the port is not listed, is the printer listed in the win32_printer entries? – m_a_s Nov 01 '14 at 19:25
  • @AlanWayne Did you ever find a solution to finding the HostName when there is no corresponding entry in Win32_TCPIPPrinterPort? Here I am 6 years later trying to solve the same problem :) – Vapid Feb 18 '20 at 09:56
  • 1
    @VapidLinus Sorry to say, I was not. As memory serves, the ultimate problem was failure of the printers to consistently put their drivers in the "recommended" location in windows. I ended up having to read the windows registery directly. In the end, I just avoided the whole thing and used the ports as windows recognized them. Hope this helps. – Alan Wayne Feb 27 '20 at 03:10
0

The printing queue has a corresponding port that is handled by the port monitor.

There are different port monitors (not only standard monitors like TCPMON and WSD but also custom and vendor-specific), as far as I know, there is no universal way to deal with all kinds of them.

From the provided port name, I assume you are dealing with the WSD port. Here things become a bit tricky, I suggest you read my answer https://stackoverflow.com/a/63705944/4700228 for the solution.

Dharman
  • 30,962
  • 25
  • 85
  • 135
metacube
  • 328
  • 3
  • 14