Home > Backend Development > C++ > body text

How to Populate a Combo-Box with Serial Port Descriptions Using WMI?

Linda Hamilton
Release: 2024-10-31 22:48:01
Original
459 people have browsed it

How to Populate a Combo-Box with Serial Port Descriptions Using WMI?

Populating Combo-Box with Serial Port Descriptions

Obtaining the descriptions of serial ports for display in a combo-box can be achieved using management objects and LINQ. A code snippet can be used to enumerate the serial ports and retrieve their descriptions:

<code class="csharp">using System.Linq;
using System.Management;

var managementSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'");
var portnames = SerialPort.GetPortNames();
var ports = managementSearcher.Get().Cast<ManagementBaseObject>().ToList().Select(port => port["Caption"].ToString());

var portList = portnames.Select(portname => portname + " - " + ports.FirstOrDefault(portDesc => portDesc.Contains(portname))).ToList();</code>
Copy after login

This code enumerates the available serial port names using SerialPort.GetPortNames() and retrieves the corresponding port descriptions using WMI. It then combines the port names and descriptions into a single list, which can be used to populate a combo-box with both the port names and their descriptions.

The above is the detailed content of How to Populate a Combo-Box with Serial Port Descriptions Using WMI?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template