简体   繁体   中英

Determine the IP Address of a Printer in C#

I would like to determine the IP address of a printer, using C# (.NET 2.0). I have only the printer share name as set up on the Windows OS, in the format \\PC Name\Printer Name . The printer is a.network printer, and has a different IP address to the PC. Does anyone have any pointers?

Thanks in advance for your help.

Regards, Andy.

Just adding an another solution here using.Net Framework 4.0 or higher

Using System.Printing

 var server = new PrintServer();
            var queues = server.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
            foreach (var queue in queues)
            {
                string printerName = queue.Name;
                string printerPort = queue.QueuePort.Name;
             }

I know this is an old post, but I had the same issue where I was able to get the Printer Port name, but not the IP. In my case I couldn't rely on the Port Name being IP_[IP Address] but found how to get hold of the actual IP from the port name.

Windows stores the information about ports in the registry under

HKLM\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\[port name]

This key contains the values set up in the port configuration page, including IP address and port number.

A quick C# example to get the IP address

using Microsoft.Win32;

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" + printerPortName, RegistryKeyPermissionCheck.Default, System.Security.AccessControl.RegistryRights.QueryValues);
if (key != null)
{
    String IP = (String)key.GetValue("IPAddress", String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
}

Check this question: How to get Printer Info in C#.NET? . I think that you have to get the property PortName from the WMI properties.

using of class WIN32_Printer is not enough here. It should be combined with Win32_TCPIPPrinterPort.

Below is the code which should help:

static void Main(string[] args)
        {
            var scope = new ManagementScope(@"\root\cimv2");
            scope.Connect();

            var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
            var results = searcher.Get();
            Console.WriteLine("Network printers list:");
            foreach (var printer in results)
            {
                var portName = printer.Properties["PortName"].Value;

                var searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_TCPIPPrinterPort where Name LIKE '" + portName + "'");
                var results2 = searcher2.Get();
                foreach (var printer2 in results2)
                {
                    Console.WriteLine("Name:" + printer.Properties["Name"].Value);
                    //Console.WriteLine("PortName:" + portName);
                    Console.WriteLine("PortNumber:" + printer2.Properties["PortNumber"].Value);
                    Console.WriteLine("HostAddress:" + printer2.Properties["HostAddress"].Value);
                }
                Console.WriteLine();
            }

            Console.ReadLine();
           }

Based on the link How to get Printer Info in .NET? (Thanks, Panos, I was already looking at the link,), I have the following solution from Panos's answer:

using System.Management;

...

string printerName = "YourPrinterName";
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection coll = searcher.Get();

foreach (ManagementObject printer in coll)
{
    string portName = printer["PortName"].ToString();
    if(portName.StartsWith("IP_"))
    {
        Console.WriteLine(string.Format("Printer IP Address: {0}", portName.Substring(3)));
    }
}

Obviously, this only works if the port name for the printer is given in the format "IP_IPAddress", which is I believe is the default.

string printerName = "POS-80C";

LocalPrintServer server = new LocalPrintServer();
PrintQueue printQueue = server.GetPrintQueue(printerName);
string portName = printQueue.QueuePort.Name;
string portNumber = "";
string hostAddress = "";

var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_TCPIPPrinterPort where Name LIKE '" + portName + "'");
var results = searcher.Get();
foreach (var printer in results)
{
    portNumber = (printer.Properties["PortNumber"].Value).ToString();
    hostAddress = (printer.Properties["HostAddress"].Value).ToString();
}

Is this printer set up in a network which has Active Directory? Or is this on your own local network with just a switch and your printer plugged into it?

If it is the former, then you should be able to query for it based on the "printer name". This article show how to get c# .net to connect to the AD . But this does require some knowledge of AD servers in your network.

This solution seems a bit long to me, but may be a good starting point?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM