Obtain Comports

I am trying to obtain the available/defined comports using this code but am getting a compiler error stating

The type or namespace name 'ManagementObjectSearcher' does not exist in the namespace 'System.Management' (are you missing an assembly reference?)

Do you guys have any ideas on how I can get this to compile?

using (var searcher = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'"))
            {
                var portnames = SerialPort.GetPortNames();
                var ports = searcher.Get().Cast<ManagementBaseObject>().ToList().Select(p => p["Caption"].ToString());

                var portList = portnames.Select(n => n + " - " + ports.FirstOrDefault(s => s.Contains(n))).ToList();


                for( int i=0; i < portList.Count; i++)
                {

                    var s = portList[i];

                    if (s.Contains("Prolific"))
                    {
                        strComPort = portnames[i];
                    }
				}
			}

I used this class in Vantage

and

Thank you Mark.

1 Like

That file will be here:
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management\v4Xsomething\System.Management.dll

Copy it into your client folder to reference.


Now that being said, what are you doing?

trying to reliably get comport my device is connected to.

Thank you. This is exactly what I needed and is working great.

1 Like

Truth What GIF by Britannia

1 Like

Nothing. I am connecting a scale to the Epicor computer and it communicates through a comport. I just want to discover it on load.

Ah ha!

You LIE!

You are connecting a scale!