Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Management;
- namespace SuperPuperNamespace
- {
- static partial class Program
- {
- static List<USBDeviceInfo> GetUSBDevices()
- {
- List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
- ManagementObjectCollection collection;
- using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity "))
- collection = searcher.Get();
- foreach (var device in collection)
- {
- devices.Add(new USBDeviceInfo(
- (string)device.GetPropertyValue("DeviceID"),
- (string)device.GetPropertyValue("PNPDeviceID"),
- (string)device.GetPropertyValue("Description"),
- (string)device.GetPropertyValue("Name")
- ));
- }
- collection.Dispose();
- return devices;
- }
- // Выдаёт первый попавшийся usb-com с нужным вид-пидом
- static string GetSerialByUSB()
- {
- var usb = GetUSBDevices();
- foreach(var U in usb)
- {
- // Вид-пид проверяем
- if(U.CheckVidPid(0x1234, 0x5678))
- {
- string Name = U.Name;
- int Start = Name.IndexOf('(');
- int End = Name.IndexOf(')');
- return Name.Substring(Start + 1, End - Start - 1);
- }
- }
- return null;
- }
- }
- class USBDeviceInfo
- {
- public USBDeviceInfo(string deviceID, string pnpDeviceID, string description, string Name)
- {
- this.DeviceID = deviceID;
- this.PnpDeviceID = pnpDeviceID;
- this.Description = description;
- this.Name = Name;
- }
- public string DeviceID { get; private set; }
- public string PnpDeviceID { get; private set; }
- public string Description { get; private set; }
- public string Name { get; private set; }
- public bool CheckVidPid(int Vid, int Pid)
- {
- var VidS = String.Format("VID_{0:X4}", Vid);
- var PidS = String.Format("PID_{0:X4}", Pid);
- return DeviceID.Contains(VidS) && DeviceID.Contains(PidS);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement