0

I tried like as:

foreach (ManagementObject queryObj in searcher.Get())
{
   Console.WriteLine("PortName: {0}", queryObj["PortName"]);
}

It gives me not found in console

1
  • I need to get number of port, COM1, COM2 etc Commented Aug 24, 2015 at 17:01

2 Answers 2

1

This should do the trick, or at least get you pointed in the right direction:

ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");
foreach (ManagementObject service in comPortSearcher.Get())
{                
   Console.WriteLine(service.ToString());
}

Baically you need to specify the right query.

Sign up to request clarification or add additional context in comments.

1 Comment

It gives me next inforation without port: \\TMOM-PC\root\CIMV2:Win32_PnPEntity.DeviceID="USB\\VID_0403&PID_6001\\A900ER2T"
0

Use this query

            string[] portnames = SerialPort.GetPortNames();


             foreach( string port in portnames)
             {

                 Console.WriteLine(port);
             }

3 Comments

For what ManagementObjectSearcher in your code if you use SerialPort.GetPortNames?
@Piubur oh you right. my mistake.edited. if it helped you could you be a litlle kind and accept the answer? :)
It does not work for me, I have ID device and want to get port on that this device works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.