diff --git a/NibblePoker.Library.ComPortHelpers/ComPortHelper.cs b/NibblePoker.Library.ComPortHelpers/ComPortHelper.cs index 1e639cc..0fca9a2 100644 --- a/NibblePoker.Library.ComPortHelpers/ComPortHelper.cs +++ b/NibblePoker.Library.ComPortHelpers/ComPortHelper.cs @@ -1,73 +1,81 @@ -using Microsoft.Win32; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Win32; using static NibblePoker.Library.RegistryHelpers.RegistryHelpers; -namespace NibblePoker.Library.ComPortWatcher; +namespace NibblePoker.Library.ComPortWatcher { + public static class ComPortHelper { + private const string RegistryKeySerial = "HARDWARE\\DEVICEMAP\\SERIALCOMM"; + private const string RegistryKeyDeviceEnum = "SYSTEM\\ControlSet001\\Enum"; -public static class ComPortHelper { - private const string RegistryKeySerial = "HARDWARE\\DEVICEMAP\\SERIALCOMM"; - private const string RegistryKeyDeviceEnum = "SYSTEM\\ControlSet001\\Enum"; - - private static readonly string[] IgnoredRegistryDeviceEnums = { - "ACPI", "ACPI_HAL", "HDAUDIO", "SCSI", "STORAGE", "SW", "SWD", "UEFI" - }; - - private const string RegistryNameFriendlyName = "FriendlyName"; - - private static void AddFriendlyNamesToPortsInfo(List comPortsInfo) { - List subKeys = GetSubKeys(Registry.LocalMachine, RegistryKeyDeviceEnum); - List intermediateSubKeys = new List(); - List finalSubKeys = new List(); - List friendlyNames = new List(); - - foreach(var subKey in subKeys.Where(subKey => !IgnoredRegistryDeviceEnums.Contains(subKey))) { - intermediateSubKeys.AddRange(GetSubKeys( - Registry.LocalMachine, - RegistryKeyDeviceEnum + "\\" + subKey, - true)); - } - subKeys.Clear(); - - foreach(string subSubKey in intermediateSubKeys) { - finalSubKeys.AddRange(GetSubKeys( - Registry.LocalMachine, - subSubKey, - true)); - } - intermediateSubKeys.Clear(); - - foreach(string finalSubKey in finalSubKeys) { - Dictionary mappedKeyValues = GetMappedKeyNameStringValue(Registry.LocalMachine, finalSubKey); - - if(mappedKeyValues.ContainsKey(RegistryNameFriendlyName)) { - friendlyNames.Add(mappedKeyValues[RegistryNameFriendlyName]); + private static readonly string[] IgnoredRegistryDeviceEnums = { + "ACPI", "ACPI_HAL", "HDAUDIO", "SCSI", "STORAGE", "SW", "SWD", "UEFI" + }; + + private const string RegistryNameFriendlyName = "FriendlyName"; + + private static void AddFriendlyNamesToPortsInfo(List comPortsInfo) { + List subKeys = GetSubKeys(Registry.LocalMachine, RegistryKeyDeviceEnum); + List intermediateSubKeys = new List(); + List finalSubKeys = new List(); + List friendlyNames = new List(); + + foreach(var subKey in subKeys.Where(subKey => !IgnoredRegistryDeviceEnums.Contains(subKey))) { + intermediateSubKeys.AddRange(GetSubKeys( + Registry.LocalMachine, + RegistryKeyDeviceEnum + "\\" + subKey, + true)); } - } - finalSubKeys.Clear(); - - foreach(ComPortInfo comPortInfo in comPortsInfo) { - foreach(string friendlyName in friendlyNames) { - if(!friendlyName.Contains("(" + comPortInfo.RawName + ")")) { - continue; + + subKeys.Clear(); + + foreach(string subSubKey in intermediateSubKeys) { + finalSubKeys.AddRange(GetSubKeys( + Registry.LocalMachine, + subSubKey, + true)); + } + + intermediateSubKeys.Clear(); + + foreach(string finalSubKey in finalSubKeys) { + Dictionary mappedKeyValues = + GetMappedKeyNameStringValue(Registry.LocalMachine, finalSubKey); + + if(mappedKeyValues.ContainsKey(RegistryNameFriendlyName)) { + friendlyNames.Add(mappedKeyValues[RegistryNameFriendlyName]); } - - comPortInfo.FriendlyName = friendlyName; - break; } - } - friendlyNames.Clear(); - } - public static List GetComList(bool addFriendlyNames = false) { - List portsInfo = new List(); - - foreach(KeyValuePair rawPortInfo in GetMappedKeyNameStringValue(Registry.LocalMachine, RegistryKeySerial)) { - portsInfo.Add(new ComPortInfo(rawPortInfo.Value, rawPortInfo.Key)); + finalSubKeys.Clear(); + + foreach(ComPortInfo comPortInfo in comPortsInfo) { + foreach(string friendlyName in friendlyNames) { + if(!friendlyName.Contains("(" + comPortInfo.RawName + ")")) { + continue; + } + + comPortInfo.FriendlyName = friendlyName; + break; + } + } + + friendlyNames.Clear(); } - - if(addFriendlyNames && portsInfo.Count > 0) { - AddFriendlyNamesToPortsInfo(portsInfo); + + public static List GetComList(bool addFriendlyNames = false) { + List portsInfo = new List(); + + foreach(KeyValuePair rawPortInfo in GetMappedKeyNameStringValue(Registry.LocalMachine, + RegistryKeySerial)) { + portsInfo.Add(new ComPortInfo(rawPortInfo.Value, rawPortInfo.Key)); + } + + if(addFriendlyNames && portsInfo.Count > 0) { + AddFriendlyNamesToPortsInfo(portsInfo); + } + + return portsInfo; } - - return portsInfo; } } diff --git a/NibblePoker.Library.ComPortHelpers/ComPortInfo.cs b/NibblePoker.Library.ComPortHelpers/ComPortInfo.cs index 4853b96..586ee2b 100644 --- a/NibblePoker.Library.ComPortHelpers/ComPortInfo.cs +++ b/NibblePoker.Library.ComPortHelpers/ComPortInfo.cs @@ -1,18 +1,18 @@ -namespace NibblePoker.Library.ComPortWatcher; +namespace NibblePoker.Library.ComPortWatcher { + public class ComPortInfo { + public string RawName; + public string DeviceName; + public string FriendlyName; -public class ComPortInfo { - public string RawName; - public string DeviceName; - public string FriendlyName; - - public ComPortInfo(string rawName, string deviceName, string friendlyName = "") { - RawName = rawName; - DeviceName = deviceName; - - if(string.IsNullOrEmpty(friendlyName)) { - FriendlyName = "No friendly name found (" + rawName + ")"; - } else { - FriendlyName = friendlyName; + public ComPortInfo(string rawName, string deviceName, string friendlyName = "") { + RawName = rawName; + DeviceName = deviceName; + + if(string.IsNullOrEmpty(friendlyName)) { + FriendlyName = "No friendly name found (" + rawName + ")"; + } else { + FriendlyName = friendlyName; + } } } -} \ No newline at end of file +} diff --git a/NibblePoker.Library.ComPortHelpers/NibblePoker.Library.ComPortHelpers.csproj b/NibblePoker.Library.ComPortHelpers/NibblePoker.Library.ComPortHelpers.csproj index b37d009..6f7e4f8 100644 --- a/NibblePoker.Library.ComPortHelpers/NibblePoker.Library.ComPortHelpers.csproj +++ b/NibblePoker.Library.ComPortHelpers/NibblePoker.Library.ComPortHelpers.csproj @@ -1,9 +1,11 @@ - net6.0-windows - enable - enable + net40;net6.0-windows;net7.0-windows;net8.0-windows + + disable + disable + NibblePoker.Library.ComPortWatcher NibblePoker.Library.ComPortHelpers Release @@ -11,7 +13,7 @@ - + diff --git a/NibblePoker.Library.ComPortHelpers/SortingOrder.cs b/NibblePoker.Library.ComPortHelpers/SortingOrder.cs index 98b3a35..3472cc3 100644 --- a/NibblePoker.Library.ComPortHelpers/SortingOrder.cs +++ b/NibblePoker.Library.ComPortHelpers/SortingOrder.cs @@ -1,7 +1,7 @@ -namespace NibblePoker.Library.ComPortWatcher; - -public enum SortingOrder { - Descending = -1, - None = 0, - Ascending = 1, +namespace NibblePoker.Library.ComPortWatcher { + public enum SortingOrder { + Descending = -1, + None = 0, + Ascending = 1, + } }