-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
SerialPort.Win32.cs
39 lines (35 loc) · 1.43 KB
/
SerialPort.Win32.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Win32;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace System.IO.Ports
{
public partial class SerialPort : Component
{
public static string[] GetPortNames()
{
// Hitting the registry for this isn't the only way to get the ports.
//
// WMI: https://msdn.microsoft.com/en-us/library/aa394413.aspx
// QueryDosDevice: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365461.aspx
//
// QueryDosDevice involves finding any ports that map to \Device\Serialx (call with null to get all, then iterate to get the actual device name)
using (RegistryKey serialKey = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM"))
{
if (serialKey != null)
{
string[] result = serialKey.GetValueNames();
for (int i = 0; i < result.Length; i++)
{
// Replace the name in the array with its value.
result[i] = (string)serialKey.GetValue(result[i]);
}
return result;
}
}
return Array.Empty<string>();
}
}
}