-1
using System;
using System.IO.Ports;

public static void getPorts()
{
    string[] ports = SerialPort.GetPortNames();

    Console.WriteLine("The following serial ports were found:");

    foreach (string port in ports)
    {
        Console.WriteLine(port);
    }
    Console.ReadLine();
}

I have problem with this simple code. I need to write out my ports because I will work with them later on (I'm using that simple function in my bigger app). Everything was working until I upgraded my Windows. Do you have any idea what could be the problem? I have also installed all drivers.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
Kubo Brehuv
  • 31
  • 2
  • 5
  • What is the exact problem? Are you getting any errors? Please add `try-catch` block in code. – Souvik Ghosh Sep 01 '17 at 07:58
  • am I right when I assume that you ports have disappeared? – Mong Zhu Sep 01 '17 at 08:08
  • Can you see the ports in Device Manager – JSR Sep 01 '17 at 08:19
  • I don't get any error but also any port names. Usually output was COM3, COM4 etc. Code runs but SerialPort.GetPortNames(); do not find any port so it don't write out any. Output is just "The following serial ports were found:" from WriteLine() – Kubo Brehuv Sep 01 '17 at 08:29
  • I don't even have Ports option like I used to (like that https://i.stack.imgur.com/Cv1pw.png), i have them listed there but not with COM names http://imgur.com/a/t405K – Kubo Brehuv Sep 01 '17 at 08:39

1 Answers1

1

The SerialPort.GetPortNames method won't list some virtual serial ports (like those provided by some USB-to-RS232 adapters etc.), since it relies on the HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM registry key as specified in the documentation. You can sse a WMI query to list all serial ports, as detailed here.

M.A. Hanin
  • 8,044
  • 33
  • 51