35 lines
891 B
C#
35 lines
891 B
C#
using System;
|
|
using System.Linq;
|
|
using Microsoft.Win32;
|
|
|
|
namespace AmebaPro3_ControlPanel.Services.Uart;
|
|
|
|
public static class PortDiscovery
|
|
{
|
|
public static string[] GetPorts()
|
|
{
|
|
try
|
|
{
|
|
using var key = Registry.LocalMachine.OpenSubKey(@"HARDWARE\\DEVICEMAP\\SERIALCOMM");
|
|
if (key == null)
|
|
{
|
|
return Array.Empty<string>();
|
|
}
|
|
|
|
var ports = key.GetValueNames()
|
|
.Select(name => key.GetValue(name))
|
|
.OfType<string>()
|
|
.Where(value => !string.IsNullOrWhiteSpace(value))
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
.OrderBy(value => value, StringComparer.OrdinalIgnoreCase)
|
|
.ToArray();
|
|
|
|
return ports;
|
|
}
|
|
catch
|
|
{
|
|
return Array.Empty<string>();
|
|
}
|
|
}
|
|
}
|