I am implementing a scenario where I should take the slot of a Usb immediately after I plug it in. 15 is what I want to take.
The code works fine after plugging in the 1st Usb. When I plug the 2nd Usb earlier than 1 minute after the 1st was plugged, I do not get any information for the 2nd Usb which is already plugged in. I will show you the code which handles the retrieve of the slot information. This method is executed after a fired event when a USB is plugged in.
Main thing: I have to wait more than 1 minute, then I can plug the 2nd Usb and take the slot. If I plug it in before waiting 1 minute after the 1st is plugged I do not take any information for the last Usb plugged.
static int GetPhysicalPort()
{
try
{
devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPSignedDriver WHERE DeviceId LIKE 'USB\\VID%' AND Description = 'USB Mass Storage Device' "))
{
collection = searcher.Get();
searcher.Dispose();
}
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceId"),
(string)device.GetPropertyValue("Description"),
(string)device.GetPropertyValue("Location")
));
}
collection.Dispose();
string LastAdded = devices[0].Location.Substring(6, 4);
Console.WriteLine(LastAdded);
return Convert.ToInt32(LastAdded);
}
catch (Exception e)
{
Console.WriteLine(e);
return 0;
}