c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
c# .net

How to bind Communications Port to dropdownlist in.net c#?

| | CSharp

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: