views:

1354

answers:

3

Hi,

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.

+1  A: 

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.

Panos
Panos, Thanks for the pointer, much appreciated.
MagicAndi
A: 

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?

Irfy
+1  A: 

Based on the link http://stackoverflow.com/questions/296182/how-to-get-printer-info-in-c-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.

MagicAndi