Ejemplo n.º 1
0
        public static UsbDevice FindDeviceByDriverKeyName(string driverKeyName, string driveName)
        {
            UsbDevice foundDevice = null;

            foreach (var controller in UsbHost.GetControllers())
            {
                UsbLogic.SearchHubDriverKeyName(controller.GetRootHub(), ref foundDevice, driverKeyName, driveName);

                if (foundDevice != null)
                {
                    break;
                }
            }

            return(foundDevice);
        }
Ejemplo n.º 2
0
        private static UsbDevice FindDeviceByInstanceID(string instanceID, string driveName)
        {
            UsbDevice foundDevice = null;

            foreach (var controller in UsbHost.GetControllers())
            {
                UsbLogic.SearchHubInstanceID(controller.GetRootHub(), ref foundDevice, instanceID, driveName);

                if (foundDevice != null)
                {
                    break;
                }
            }

            return(foundDevice);
        }
Ejemplo n.º 3
0
 public static IEnumerable <UsbDevice> GetDrives()
 {
     foreach (var drive in DriveInfo.GetDrives().Select(x => UsbLogic.FindDriveLetter(x.Name)).Where(x => x != null))
     {
         yield return(new UsbDevice
         {
             Address = drive.Address,
             DriveName = drive.DriveName,
             PortNumber = drive.PortNumber,
             DriverKey = drive.DriverKey,
             HubDevicePath = drive.HubDevicePath,
             InstanceID = drive.InstanceID,
             Name = drive.Name,
             Manufacturer = drive.Manufacturer,
             Product = drive.Product,
             SerialNumber = drive.SerialNumber,
         });
     }
 }
Ejemplo n.º 4
0
 private static IEnumerable <UsbDevice> ListHub(UsbHub hub, string driveName)
 {
     foreach (var port in hub.GetPorts())
     {
         if (port.IsHub)
         {
             foreach (var device in UsbLogic.ListHub(port.GetHub(driveName), driveName))
             {
                 yield return(device);
             }
         }
         else
         {
             if (port.IsDeviceConnected)
             {
                 yield return(port.GetDevice(driveName));
             }
         }
     }
 }
Ejemplo n.º 5
0
        private static void SearchHubDriverKeyName(UsbHub hub, ref UsbDevice foundDevice, string driverKeyName, string driveName)
        {
            foreach (var port in hub.GetPorts())
            {
                if (port.IsHub)
                {
                    UsbLogic.SearchHubDriverKeyName(port.GetHub(driveName), ref foundDevice, driverKeyName, driveName);
                }
                else
                {
                    if (port.IsDeviceConnected)
                    {
                        var device = port.GetDevice(driveName);

                        if (device.DriverKey == driverKeyName)
                        {
                            foundDevice = device;
                            break;
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private static void SearchHubInstanceID(UsbHub hub, ref UsbDevice foundDevice, string instanceID, string driveName)
        {
            foreach (var port in hub.GetPorts())
            {
                if (port.IsHub)
                {
                    UsbLogic.SearchHubInstanceID(port.GetHub(driveName), ref foundDevice, instanceID, driveName);
                }
                else
                {
                    if (port.IsDeviceConnected)
                    {
                        var device = port.GetDevice(driveName);

                        if (device.InstanceID == instanceID)
                        {
                            foundDevice = device;
                            break;
                        }
                    }
                }
            }
        }
Ejemplo n.º 7
0
 public static IEnumerable <UsbDevice> GetConnectedDevices(string driveName)
 {
     foreach (var device in UsbHost.GetControllers().Select(x => x.GetRootHub()).SelectMany(x => UsbLogic.ListHub(x, driveName)))
     {
         yield return(device);
     }
 }
Ejemplo n.º 8
0
        public static UsbDevice FindDriveLetter(string driveName)
        {
            UsbDevice foundDevice = null;
            var       instanceID  = string.Empty;

            var devNum = UsbLogic.GetDeviceNumber($@"\\.\{driveName.TrimEnd('\\')}");

            if (devNum < default(int))
            {
                return(foundDevice);
            }

            var diskGUID = new Guid(UsbConstants.GUID_DEVINTERFACE_DISK);

            var h = SetupApi.SetupDiGetClassDevs(ref diskGUID, default(int), IntPtr.Zero, UsbConstants.DIGCF_PRESENT | UsbConstants.DIGCF_DEVICEINTERFACE);

            if (h.ToInt32() != UsbConstants.INVALID_HANDLE_VALUE)
            {
                var i       = default(int);
                var success = true;
                do
                {
                    var dia = new SpDeviceInterfaceData();
                    dia.cbSize = Marshal.SizeOf(dia);

                    success = SetupApi.SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref diskGUID, i, ref dia);
                    if (success)
                    {
                        var da = new SpDevinfoData();
                        da.cbSize = Marshal.SizeOf(da);

                        var didd = new SpDeviceInterfaceDetailData
                        {
                            cbSize = 4 + Marshal.SystemDefaultCharSize,
                        };

                        var nRequiredSize = default(int);
                        int nBytes        = UsbConstants.BUFFER_SIZE;
                        if (SetupApi.SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, ref nRequiredSize, ref da))
                        {
                            if (UsbLogic.GetDeviceNumber(didd.DevicePath) == devNum)
                            {
                                SetupApi.CM_Get_Parent(out IntPtr ptrPrevious, da.DevInst, default(int));

                                var ptrInstanceBuf = Marshal.AllocHGlobal(nBytes);
                                SetupApi.CM_Get_Device_ID(ptrPrevious, ptrInstanceBuf, nBytes, default(int));
                                instanceID = Marshal.PtrToStringAuto(ptrInstanceBuf);

                                Marshal.FreeHGlobal(ptrInstanceBuf);
                                break;
                            }
                        }
                    }
                    i++;
                } while (success);
                SetupApi.SetupDiDestroyDeviceInfoList(h);
            }

            if (instanceID.StartsWith("USB\\"))
            {
                foundDevice = UsbLogic.FindDeviceByInstanceID(instanceID, driveName);
            }

            return(foundDevice);
        }