I had been using the code below for a console application in Visual Studio on Windows 8 to return the description and device ID of connected serial devices. I was using a modified version of this in an application I'm creating to automatically detect the COM port of an Arduino. It no longer returns anything since I've done a fresh install with Windows 10. I have a USB to serial AVR programmer that still shows up using this code however. I checked the registry and the Arduino is listed in SERIALCOMM, the Arduino is showing as 'USB Serial Port (COM6)' under 'Ports (COM & LPT)' in Device Manager and I can program the Arduino using the Arduino software. I have no idea why it isn't working any more.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.IO.Ports;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
ManagementScope connectionScope = new ManagementScope();
SelectQuery serialQuery = new SelectQuery("SELECT * FROM Win32_SerialPort");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(connectionScope, serialQuery);
try
{
foreach (ManagementObject item in searcher.Get())
{
string desc = item["Description"].ToString();
string deviceId = item["DeviceID"].ToString();
Console.WriteLine(desc);
Console.WriteLine(deviceId);
}
}
catch (ManagementException e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}
It might also be relevant that while trying to find a solution I found the following implementation to find port names using MSSerial_PortName and I got an Access Denied error.
using System;
using System.Management;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
try
{
ManagementObjectSearcher MOSearcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");
foreach (ManagementObject MOject in MOSearcher.Get())
{
Console.WriteLine(MOject["PortName"]);
}
}
catch (ManagementException me)
{
Console.WriteLine("An error occurred while querying for WMI data: " + me.Message);
}
Console.ReadKey();
}
}
}