laatzen/Common/Ui/Common.UI/Infrastructure/DeviceManager.cs
2024-12-03 17:03:33 +01:00

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();
}
}