Question: I want to get my printer's port number in c#

I have tried using Win32\_Printer and PrinterSettings

```
//Required namespaces
using System.Management;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Printing;
using System.IO.Ports;
using System.Net;

            String printerName = "My printer name";
            //String query = String.Format("Select Name, PortName from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
            String query = String.Format("Select * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
            ManagementObjectSearcher printers = new ManagementObjectSearcher(query);
            foreach (ManagementObject printer in printers.Get())
            {
                DeviceName = (string)printer.GetPropertyValue("Name");
                //Console.WriteLine(DeviceName);
                PortName = (string)printer.GetPropertyValue("PortName");
                //Console.WriteLine(PortName);
            } 

        PrinterSettings ps = new PrinterSettings();
        ps.PrinterName = printerName;

```

### I can get is port name , but not the port number, please help.

Answer: 
Using Microsoft's [WMI Code Creator v1.0](https://www.microsoft.com/en-gb/download/details.aspx%3Fid%3D8572) tool, I found you can access PortNumber for Standard TCP/IP Ports via Win32\_TCPIPPrinterPort.

```
using System;
using System.Management;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_TCPIPPrinterPort");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_TCPIPPrinterPort instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Name: {0}", queryObj["Name"]);
                    Console.WriteLine("PortNumber: {0}", queryObj["PortNumber"]);
                }
            }
            catch (ManagementException e)
            {
               Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
            }

            Console.ReadKey();
        }
    }
}

```
`ManagementObject` has a `properties` property, which returns collection of `PropertyData` objects. 

<https://msdn.microsoft.com/en-us/library/system.management.managementbaseobject.properties(v=vs.110).aspx>

From `PropertyData` you can get `Name` and `Value`. Just iterate all properties, you may find the port number.

<https://msdn.microsoft.com/en-us/library/system.management.propertydata(v=vs.110).aspx>

**Below is pseudo code: (untested)**

```
        foreach (ManagementObject printer in printers.Get())
        {
            foreach (PropertyData property in printer.properties)
            {
                 Console.WriteLine(property.Name);
                 Console.WriteLine(property.Value);

            }
        } 

```