31 lines
913 B
C#
31 lines
913 B
C#
namespace Common.UI.Infrastructure
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO.Ports;
|
|
using System.Linq;
|
|
using System.Management;
|
|
|
|
internal class DeviceManager
|
|
{
|
|
private readonly ManagementEventWatcher deviceWatcher;
|
|
|
|
public DeviceManager()
|
|
{
|
|
this.deviceWatcher = new ManagementEventWatcher("SELECT * FROM Win32_DeviceChangeEvent");
|
|
this.deviceWatcher.EventArrived += DeviceWatcher_EventArrived;
|
|
this.deviceWatcher.Start();
|
|
}
|
|
|
|
public event Action COMPortsChanged;
|
|
|
|
public IEnumerable<string> COMPorts
|
|
=> SerialPort.GetPortNames()
|
|
.OrderBy(x => x.Length)
|
|
.ThenBy(x => x);
|
|
|
|
private void DeviceWatcher_EventArrived(object _1, EventArrivedEventArgs _2)
|
|
=> this.COMPortsChanged?.Invoke();
|
|
}
|
|
}
|