You must add System.Management package (not System.Management.dll for .NET Framework)
(I tested with 10.0.0-preview.3.25171.5 and it worked on Windows 10 22H2)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I made my app, that connects to COM ports to send data
is working perfectly
I use this
public void GetComPorts() {
ComPorts.Clear();
foreach(var port in SerialPort.GetPortNames()) {
ComPorts.Add(port);
}
}
but if I want to get the full name with this
using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%'");
foreach (var queryObj in searcher.Get())
{
var deviceName = queryObj["Name"]?.ToString();
if (deviceName != null)
{
ComPorts.Add(deviceName);
}
}
my app crash
I just want to get the full names, without the com
For example, I am using com0com, to simulate. I want to get
com0com - serial port emulator
note: I want to concatenate the com port, just for development purposes
Window code behind
mainViewModel.ComPortsListAction += async (comPorts) => {
if(comPorts != null) {
var portsListView = new ListView {
ItemsSource = comPorts,
SelectionMode = ListViewSelectionMode.Single,
};
var comDlg = new ContentDialog {
Title = "Select COM Port",
Content = portsListView,
PrimaryButtonText = Constants.ok,
CloseButtonText = Constants.cancel,
XamlRoot = Content.XamlRoot
};
if(await comDlg.ShowAsync() == ContentDialogResult.Primary) {
if(portsListView.SelectedItem is string selectedPort) {
Constants.printerPort = selectedPort;
Constants.OpenedPort = new SerialPort(selectedPort, Constants.printerBaudRate);
Constants.OpenedPort.Open();
mainViewModel.IsComPortConnected = true;
if(mainViewModel.IsComPortConnected) {
mainViewModel.ComPortStatus = Constants.connectedPrinter;
ConnectedPrinterIndictor.Fill = new SolidColorBrush(Colors.Green);
}
else {
mainViewModel.ComPortStatus = Constants.discconnectedPrinter;
ConnectedPrinterIndictor.Fill = new SolidColorBrush(Colors.Red);
}
}
}
I create an action and send it to the window
[RelayCommand]
void GetComPortsCommand() {
GetComPorts();
ComPortsListAction?.Invoke(ComPorts);
}
You must add System.Management package (not System.Management.dll for .NET Framework)
(I tested with 10.0.0-preview.3.25171.5 and it worked on Windows 10 22H2)