0

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. 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;
        }
edgj4718
  • 47
  • 9
  • Do you mean you can get fired events for two USB devices but `GetPropertyValue` return `NULL` for the second device? Or you can't receive fired event for the second device? – Rita Han Aug 25 '20 at 08:46
  • Exactly, the Win32_PnPSignedDriver does not update. For the second device I got still the slot of the 1st one. This method is executed immediately after a USB is plugged in. – edgj4718 Aug 26 '20 at 04:58
  • 1
    I use this PowerShell command: `Get-CimInstance Win32_PnPSignedDriver | Select-Object DeviceId,Description | where DeviceId -like "USB\VID*" | where description -like "USB Mass Storage Device"`. It does take time to get new update. Can you check if [`WM_DEVICECHANGE`](https://learn.microsoft.com/en-us/windows/win32/devio/wm-devicechange) helps? [Related issue](https://stackoverflow.com/questions/62712837/get-usb-volume-path-from-guid/62806756#62806756). – Rita Han Aug 27 '20 at 08:04
  • Yes it takes time to load and that is my issue. WM_DEVICECHANGE API does not help fur my purpose. I am actually using it to trigger the GetPhysicalPort() method. – edgj4718 Aug 27 '20 at 08:26
  • Since `WM_DEVICECHANGE` message is posted immediately when the device is inserted, and from there you can get drive path like E: / F: the USB device mapped to. So if you want to read from or write to that storage, it can be achieved more quickly than using `Win32_PnPSignedDriver`. Or could you share what do you want to do with the plugged USB slot? – Rita Han Aug 28 '20 at 03:28
  • Hi Rita. I already know how to take the path and I do it with the WM_DEVICECHANGE. What I want to have is the slot where the USB is plugged, and that information I cannot have it with the WM_DEVICECHANGE. – edgj4718 Aug 28 '20 at 11:23
  • I am consulting this issue with the related engineer to see if there is a workaround for you and update to you if there is any progress. – Rita Han Sep 01 '20 at 07:09
  • Does the issue still exists even if the app is restarted? – Rita Han Sep 18 '20 at 05:24
  • Unfortunately yes, I am afraid that the API is not the correct one. – edgj4718 Sep 23 '20 at 07:49
  • My colleague is working on this issue. Please let me know if you are still looking for a solution. – Rita Han Oct 09 '20 at 05:55

1 Answers1

0

As per WMI Win32_PnPSignedDriver class it would be the application which would be taking that much time to query or give results for the USB inserted second time. This depends on application to application as the pole rate or response rate of that application running this WMI query would be designed it such a way to give results in 1-2 min.

We could check the same in terms of OS level or WMI level to see if WMI as a whole is taking time to give query results on the machine.

  1. Open Run command and type WBEMTEST.
  2. Click on connect as it will connect the CIMV2 Namespace. Under this namespace lies the class Win32_PnPSignedDriver
  3. As an when we query this class with the query select * from win32_PnPSignedDriver it would shows us some results instantly like for eg 60-70.
  4. Now when you close it and again run he same query it would show some more or the same results depending on the OS etc.

The above will show us that WMI query or WMI as a whole is working the way it should be on the machine.

The issue could be caused by the application which is taking this much time to read the contents of the second USB as per the WMI query it is querying.

Rita Han
  • 9,574
  • 1
  • 11
  • 24