110,556
社区成员
发帖
与我相关
我的任务
分享
string [] spname=System.IO.Ports.SerialPort.GetPortNames();
if (spname.Length > 0)
{
foreach (string spName in spname)
comboBox_spName.Items.Add(spName);//得到所有串口号
}
private static void TestRegex18()
{
string yourStr = @"通讯端口(COM1),通讯端口(COM2),通讯端口(COM3),Usb To Serial com port(COM4)";
MatchCollection mc = Regex.Matches(yourStr,"(?<=COM)\\d+");
List<int> ports = new List<int>();
foreach (Match item in mc)
{
ports.Add(int.Parse(item.Value));
}
foreach (int port in ports)
{
Console.WriteLine(port);
}
//ports就是你要的
}