How to scan for COM ports in C#?

前端 未结 3 766
梦谈多话
梦谈多话 2021-02-14 07:19

Does C# provide an effective means of scanning the available COM ports? I would like to have a dropdown list in my application wherein the user can select one of the detected C

3条回答
  •  离开以前
    2021-02-14 07:36

    Use WMI through the System.Management namespace. A quick Google finds this code:

    using System;
    using System.Management;
    
    public class Foo 
    {
        public static void Main() 
        {
           var instances = new ManagementClass("Win32_SerialPort").GetInstances();
           foreach ( ManagementObject port in instances )
           {
               Console.WriteLine("{0}: {1}", port["deviceid"], port["name"]);
           }
    }
    

提交回复
热议问题