显示空结果c#

本文关键字:结果 显示 | 更新日期: 2023-09-27 18:04:34

伙计们,我正在使用以下代码,但它没有显示输出,也没有显示任何错误。请帮助。

private void button1_Click(object sender, EventArgs e)
{
    string[] ports = SerialPort.GetPortNames();
    //Display each port name to the console.
    foreach (string port in ports)
    {
        listBox1.Items.Add(port);
        //_serialport.open();
    }
}

显示空结果c#

using System;
namespace listSerial
{
    class Program
    {
        public static void Main(string[] args)
        {
            string[] names = null;
            try
            {
                names = System.IO.Ports.SerialPort.GetPortNames();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            if(names!=null)
            {
                int portnum = names.Length;
                if (portnum != 0)
                {
                    for (int i = 0; i < names.Length; i++)
                        Console.WriteLine(names[i]);
                }
                else
                {
                    Console.WriteLine("NO_COM");
                }
            }
        }
    }
}