getting the full names of my com port makes my app crash

Eduardo Gomez 3,631 Reputation points
2025-04-14T10:09:04.7133333+00:00

I made my app, that connects to COM ports to send dataCom ports

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

User's image

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

Com ports Broken

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);
}
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
877 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 89,136 Reputation points
    2025-04-14T15:32:21.9733333+00:00

    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)


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.