So far i have this. I have had to resort to ManagementObjectSearcher to get the ipaddress of the port.
I will accept this answer for now. If anyone knows a way of doing this without ManagementObjectSearcher I will accept that answer instead.
public virtual IEnumerable<Printer> GetPrinters()
{
var ports = new Dictionary<string, IPAddress>();
var selectQuery = new SelectQuery("Win32_TCPIPPrinterPort");
selectQuery.SelectedProperties.Add("CreationClassName");
selectQuery.SelectedProperties.Add("Name");
selectQuery.SelectedProperties.Add("HostAddress");
selectQuery.Condition = "CreationClassName = 'Win32_TCPIPPrinterPort'";
using (var searcher = new ManagementObjectSearcher(Scope, selectQuery))
{
var objectCollection = searcher.Get();
foreach (ManagementObject managementObjectCollection in objectCollection)
{
var portAddress = IPAddress.Parse(managementObjectCollection.GetProperty<string>("HostAddress"));
ports.Add(managementObjectCollection.GetProperty<string>("Name"), portAddress);
}
}
using (var printServer = new PrintServer(string.Format(@"\\{0}", PrinterServerName)))
{
foreach (var queue in printServer.GetPrintQueues())
{
if (!queue.IsShared)
{
continue;
}
yield return new Printer
{
Location = queue.Location,
Name = queue.Name,
PortName = queue.QueuePort.Name,
PortAddress = ports[queue.QueuePort.Name]
};
}
}
}