Added .NET Framework 4 target for "ComPortHelper" library

Update ComPortHelper.cs, ComPortInfo.cs, and 2 more files...
This commit is contained in:
2024-05-24 16:35:15 +02:00
parent 20052c3fb4
commit 6e4462c8fa
4 changed files with 97 additions and 87 deletions
@@ -1,73 +1,81 @@
using Microsoft.Win32; using System.Collections.Generic;
using System.Linq;
using Microsoft.Win32;
using static NibblePoker.Library.RegistryHelpers.RegistryHelpers; 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 static readonly string[] IgnoredRegistryDeviceEnums = {
private const string RegistryKeySerial = "HARDWARE\\DEVICEMAP\\SERIALCOMM"; "ACPI", "ACPI_HAL", "HDAUDIO", "SCSI", "STORAGE", "SW", "SWD", "UEFI"
private const string RegistryKeyDeviceEnum = "SYSTEM\\ControlSet001\\Enum"; };
private static readonly string[] IgnoredRegistryDeviceEnums = { private const string RegistryNameFriendlyName = "FriendlyName";
"ACPI", "ACPI_HAL", "HDAUDIO", "SCSI", "STORAGE", "SW", "SWD", "UEFI"
}; private static void AddFriendlyNamesToPortsInfo(List<ComPortInfo> comPortsInfo) {
List<string> subKeys = GetSubKeys(Registry.LocalMachine, RegistryKeyDeviceEnum);
private const string RegistryNameFriendlyName = "FriendlyName"; List<string> intermediateSubKeys = new List<string>();
List<string> finalSubKeys = new List<string>();
private static void AddFriendlyNamesToPortsInfo(List<ComPortInfo> comPortsInfo) { List<string> friendlyNames = new List<string>();
List<string> subKeys = GetSubKeys(Registry.LocalMachine, RegistryKeyDeviceEnum);
List<string> intermediateSubKeys = new List<string>(); foreach(var subKey in subKeys.Where(subKey => !IgnoredRegistryDeviceEnums.Contains(subKey))) {
List<string> finalSubKeys = new List<string>(); intermediateSubKeys.AddRange(GetSubKeys(
List<string> friendlyNames = new List<string>(); Registry.LocalMachine,
RegistryKeyDeviceEnum + "\\" + subKey,
foreach(var subKey in subKeys.Where(subKey => !IgnoredRegistryDeviceEnums.Contains(subKey))) { true));
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<string, string> mappedKeyValues = GetMappedKeyNameStringValue(Registry.LocalMachine, finalSubKey);
if(mappedKeyValues.ContainsKey(RegistryNameFriendlyName)) {
friendlyNames.Add(mappedKeyValues[RegistryNameFriendlyName]);
} }
}
finalSubKeys.Clear(); subKeys.Clear();
foreach(ComPortInfo comPortInfo in comPortsInfo) { foreach(string subSubKey in intermediateSubKeys) {
foreach(string friendlyName in friendlyNames) { finalSubKeys.AddRange(GetSubKeys(
if(!friendlyName.Contains("(" + comPortInfo.RawName + ")")) { Registry.LocalMachine,
continue; subSubKey,
true));
}
intermediateSubKeys.Clear();
foreach(string finalSubKey in finalSubKeys) {
Dictionary<string, string> mappedKeyValues =
GetMappedKeyNameStringValue(Registry.LocalMachine, finalSubKey);
if(mappedKeyValues.ContainsKey(RegistryNameFriendlyName)) {
friendlyNames.Add(mappedKeyValues[RegistryNameFriendlyName]);
} }
comPortInfo.FriendlyName = friendlyName;
break;
} }
}
friendlyNames.Clear();
}
public static List<ComPortInfo> GetComList(bool addFriendlyNames = false) { finalSubKeys.Clear();
List<ComPortInfo> portsInfo = new List<ComPortInfo>();
foreach(ComPortInfo comPortInfo in comPortsInfo) {
foreach(KeyValuePair<string, string> rawPortInfo in GetMappedKeyNameStringValue(Registry.LocalMachine, RegistryKeySerial)) { foreach(string friendlyName in friendlyNames) {
portsInfo.Add(new ComPortInfo(rawPortInfo.Value, rawPortInfo.Key)); if(!friendlyName.Contains("(" + comPortInfo.RawName + ")")) {
continue;
}
comPortInfo.FriendlyName = friendlyName;
break;
}
}
friendlyNames.Clear();
} }
if(addFriendlyNames && portsInfo.Count > 0) { public static List<ComPortInfo> GetComList(bool addFriendlyNames = false) {
AddFriendlyNamesToPortsInfo(portsInfo); List<ComPortInfo> portsInfo = new List<ComPortInfo>();
foreach(KeyValuePair<string, string> 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;
} }
} }
@@ -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 ComPortInfo(string rawName, string deviceName, string friendlyName = "") {
public string RawName; RawName = rawName;
public string DeviceName; DeviceName = deviceName;
public string FriendlyName;
if(string.IsNullOrEmpty(friendlyName)) {
public ComPortInfo(string rawName, string deviceName, string friendlyName = "") { FriendlyName = "No friendly name found (" + rawName + ")";
RawName = rawName; } else {
DeviceName = deviceName; FriendlyName = friendlyName;
}
if(string.IsNullOrEmpty(friendlyName)) {
FriendlyName = "No friendly name found (" + rawName + ")";
} else {
FriendlyName = friendlyName;
} }
} }
} }
@@ -1,9 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework> <TargetFrameworks>net40;net6.0-windows;net7.0-windows;net8.0-windows</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<RootNamespace>NibblePoker.Library.ComPortWatcher</RootNamespace> <RootNamespace>NibblePoker.Library.ComPortWatcher</RootNamespace>
<AssemblyName>NibblePoker.Library.ComPortHelpers</AssemblyName> <AssemblyName>NibblePoker.Library.ComPortHelpers</AssemblyName>
<Configurations>Release</Configurations> <Configurations>Release</Configurations>
@@ -11,7 +13,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="NibblePoker.Library.RegistryHelpers" Version="0.0.1" /> <PackageReference Include="NibblePoker.Library.RegistryHelpers" Version="1.0.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>
@@ -1,7 +1,7 @@
namespace NibblePoker.Library.ComPortWatcher; namespace NibblePoker.Library.ComPortWatcher {
public enum SortingOrder {
public enum SortingOrder { Descending = -1,
Descending = -1, None = 0,
None = 0, Ascending = 1,
Ascending = 1, }
} }