/
EnumComPort.cs
74 lines (61 loc) · 2.06 KB
/
EnumComPort.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Management;
using System.Collections;
using System.Text.RegularExpressions;
namespace enumcomport
{
class ComPortComparer : System.Collections.IComparer
{
Regex reg = new Regex("COM(?<num>\\d+)");
public int Compare(object a, object b)
{
int a_num = int.Parse(reg.Match((string)a).Groups["num"].Value);
int b_num = int.Parse(reg.Match((string)b).Groups["num"].Value);
if (a_num == b_num)
{
return 0;
}
if (a_num < b_num)
{
return -1;
}
return 1;
}
}
class EnumComPort
{
public static string[] getComPorts()
{
var list = new ArrayList();
ManagementClass win32_pnpentity = new ManagementClass("Win32_PnPEntity");
ManagementObjectCollection col = win32_pnpentity.GetInstances();
Regex reg = new Regex(".+\\((?<port>COM\\d+)\\)");
foreach (ManagementObject obj in col)
{
// name : "USB Serial Port(COM??)"
string name = (string)obj.GetPropertyValue("name");
if (name != null && name.Contains("(COM"))
{
// "USB Serial Port(COM??)" -> COM??
Match m = reg.Match(name);
string port = m.Groups["port"].Value;
// description : "USB Serial Port"
string desc = (string)obj.GetPropertyValue("Description");
// result string : "COM?? (USB Serial Port)"
list.Add(port + " (" + desc + ")");
}
}
ComPortComparer comp = new ComPortComparer();
list.Sort(comp);
return (string[])list.ToArray(typeof(string));
}
public static void Main()
{
string[] ports = getComPorts();
foreach (string port in ports)
{
Console.WriteLine(port);
}
}
}
}