using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Management; namespace WindowsFormsApplication4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { ManagementObjectSearcher MOSearcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName"); foreach (ManagementObject MOject in MOSearcher.Get()) { listBox1.Items.Add(MOject["InstanceName"]);//MSSerial_PortName instance listBox1.Items.Add(MOject["PortName"]);//MSSerial_PortName instance //If the serial port's instance name contains USB it must be a USB to serial device if (MOject["InstanceName"].ToString().Contains("USB")) { listBox1.Items.Add(MOject["PortName"] + " is a USB to Serial Adapter/Converter"); } } } catch (ManagementException me) { MessageBox.Show("An error occurred while querying for WMI data: " + me.Message); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)