Search code examples
c#.net-6.0

C# .Net 6.0 System.IO.Ports is depricated. How can I use SerialPort?


I am C# programmer and using VS 2022, with .NET Framework is no problem using System.IO.Ports.SerialPort in order to use .GetPortNames() for collect available serial port list.

for example;

ComboBox CMS = new ComboBox();
string[] comlist = System.IO.SerialPort.GetPortNames();
CMS.Items.Clear();
//ComboBox item add if serial port is exist
if (comlist.Length > 0) CMS.Items.AddRange(comlist);

However, this code is not working on .NET 6.0. System.IO is exist but .Ports does not. So Ports.SerialPort... all removed.

How can I get Serial Ports list with .NET 6.0? I've searched and google a lot but can't find solution.


Solution

  • Try adding the System.IO.Ports NuGet package:

    <PackageReference Include="System.IO.Ports" Version="7.0.0" />
    

    Also note that the fully qualified name is System.IO.Ports.SerialPort, not System.IO.SerialPort