In this article we will discuss, How to bind Communications Port to dropdownlist in.net c#. Communication port is a physical port, but also to virtual ports, such as ports created by Bluetooth or USB to serial adapters.
program:
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;
using System.IO.Ports;
namespace cSharpPortFinder
{
public partial class finder : Form
{
public finder()
{
InitializeComponent();
using (var searcher = new ManagementObjectSearcher
("SELECT * FROM WIN32_SerialPort"))
{
string[] portnames = SerialPort.GetPortNames();
var ports = searcher.Get().Cast<ManagementBaseObject>().ToList();
var tList = (from n in portnames
join p in ports on n equals p["DeviceID"].ToString()
select n + " - " + p["Caption"]).ToList();
foreach (string s in tList)
{
cboPortList.Items.Add(s);
cboPortList.SelectedIndex = 0; }
}
}
}
}
Output: