Skip to content

Instantly share code, notes, and snippets.

@Joooohee
Last active September 28, 2024 03:26
Show Gist options
  • Save Joooohee/47a520e466e9ae610715393bc1902ce3 to your computer and use it in GitHub Desktop.
Save Joooohee/47a520e466e9ae610715393bc1902ce3 to your computer and use it in GitHub Desktop.
장치VID/PID를 사용해서 PC에 연결되어 있는 특정 장치 찾기
private string getDeviceComport(string vid, string pid)
{
var comport = String.Empty;
using (var searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_SerialPort"))
{
var portnames = SerialPort.GetPortNames();
var ports = searcher.Get().Cast<ManagementObjectSearcher>().ToList();
var port = ports.FirstOrDefault(w => w["PNPDeviceID"].ToString().Contains($"{vid}&{pid}"));
if(String.IsNullOrEmpty(port))
{
comport = port["DeviceID"].ToString();
}
}
return comport;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment