none
How do I get the "Device Instance Id" of USB flash drive?

    Question

  • Hello,

    How do I get the "Device Instance Id" of USB flash drive?

    WPF C# code to accomplish this will be greatly appreciated.

    I'm trying again to get an answer to the above question.  It appears that some do not know what I mean by "Device Instance Id".  Here is how you find it on a computer with the XP operating system:

    If you go to it the wrong way you will not find it.  You have to follow these steps (on XP):

    1) Make sure you have a USB flash drive inserted into one of the USB ports.

    2) Click "Start" then right click "MyComputer" then click "Manage".

    3) In the "Computer Management" window click "Device Manager".

    4) In the right pane expand the "Disk drives".

    5) Right click on "USB Flash Disk USB Device" (Will NOT be there if no USB Flash drive inserted).

    6) Click "Properties".

    7) Click the "Details" tab.

    8) The drop down should already have "Device Instance Id" showing, if not, select it.

    9) The contents of the text box below contain the information I want.

    Again, if you get to this window a different way, the "Details" tab may not appear.

    Again, if you do not have a USB flash drive plugged into your computer, the "USB Flash Disk USB Device" will not appear.

    This is what has been suggested before, but does not work.  I need working code, not links to text that suggests how to do it, they don't seem to work in WPF.  Besides, if it did, why would they not post the actual code they used. Finding code for none WPF programs is simple, but no code for WPF that gets what I need.

    Here is what I have already tried:

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
          ManagementObjectSearcher moSearch = new ManagementObjectSearcher ("Select * from Win32_LogicalDisk where Name='I:'");
          foreach (var mo in moSearch.Get())
          {
            tbID.Text = mo["VolumeSerialNumber"].ToString();
            MessageBox.Show("Device Instance Id: " + tbID.Text);<br/>
           }
        }
    
    
    I have replaced the "tbID.Text = ..." with the following (The result is the comment on the right):

    //    tbID.Text = mo["Caption"].ToString();     // I:
    //    tbID.Text = mo["CreationClassName"].ToString();  // Win32_LogicalDisk
    //    tbID.Text = mo["Description"].ToString();    // Removable Disk
    //    tbID.Text = mo["DeviceID"].ToString();     // I:
    //    tbID.Text = mo["ErrorDescription"].ToString();   // *** Causes crash
    //    tbID.Text = mo["ErrorMethodology"].ToString();   // *** Causes crash
    //    tbID.Text = mo["FileSystem"].ToString();    // FAT
    //    tbID.Text = mo["Name"].ToString();      // I:
    //    tbID.Text = mo["PNPDeviceID"].ToString();    // *** Causes crash
    //    tbID.Text = mo["ProviderName"].ToString();    // *** Causes crash
    //    tbID.Text = mo["Purpose"].ToString();     // *** Causes crash
    //    tbID.Text = mo["Status"].ToString();     // *** Causes crash
    //    tbID.Text = mo["SystemCreationClassName"].ToString(); // Win32_ComputerSystem
    //    tbID.Text = mo["SystemName"].ToString();    // RFM01DEV
    //    tbID.Text = mo["VolumeName"].ToString();    // USB DISK
    //    tbID.Text = mo["VolumeSerialNumber"].ToString();  // DE7AC015
    
    

    None of the above give me what I'm looking for.

    Regards,

    Robert

     

    Friday, September 03, 2010 2:28 PM

All replies

  • You might want to take a look at the .NET USB library
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    Friday, September 03, 2010 6:06 PM
  • Hello Geert,

    Thanks, but no thanks.  I am not interested in any products where I must abide by their license agreement, no mater how liberal it is.

    I'm beginning to think that what I want must be an extremely difficult thing to do in WPF.

    Regards,

    Robert

    Friday, September 03, 2010 6:45 PM
  • Hey Robert

    Just came across this .. haven't tried it myself but you can give it a shot.

    http://www.codeproject.com/KB/system/EnumDeviceProperties.aspx

    AT

    Friday, September 03, 2010 7:52 PM
  • I have looked at 2 different USB drives from 2 different manufacturers and NEITHER one shows a "Device Instance ID" in the Details tab.  It shows all kinds of other goodies but not an entry that is JUST "Device Instance ID".

     

    There is however a "Device Instance Path" of which the last part, after the last "\", is the Instance ID.  The part prior to the last "\", but after the 1st "\",  is the Device ID.  The two together represent the Device Instance ID.

     

    http://msdn.microsoft.com/en-us/library/ff541327(VS.85).aspx

     

    http://msdn.microsoft.com/en-us/library/ff547656(v=VS.85).aspx

     

    Here is a Victorinox USB drive:

    USBSTOR\DISK&VEN_SWISSBIT&PROD_VICTORINOX_2.0&REV_2.00\10002847F5000546&0

    DeviceID = DISK&VEN_SWISSBIT&PROD_VICTORINOX_2.0&REV_2.00

    InstanceID = 10002847F5000546&0

     


    May we all make money in the sequel.
    Friday, September 03, 2010 8:01 PM
  • Hello Console.WriteLine,

    You might take notice that the article you referred me to was posted 27-Apr-2004 for VC6 and Win2K.  WPF wasn't introduced until 2006.  Finding code for none-WPF is easy.  Its finding code for WPF that is difficult.

    Regards,

    Robert

    Saturday, September 04, 2010 4:12 AM
  • Hello Dave,

    It is "Device Instance Id" on Windows XP Pro SP3.  I found no "Device Instance Path", but maybe that's the name used by newer Windows operating systems such as Vista and 7, but I don't know.

    What operating system are you using?

    Regards,

    Robert

    Saturday, September 04, 2010 4:21 AM
  • Just plugged one in, followed your instructions and Device Instance Id does not show in the dropdown.  I am on Win7.

    LS

    Saturday, September 04, 2010 10:26 PM
  • Hello sqlguy,

    I just checked with two different USB flash drives and they both have a "Device Instance Id" on WinXP.

    Maybe it is called something else under Win7, perhaps "Device Instance Path".

    Regards,

    Robert

    Saturday, September 04, 2010 11:30 PM
  • I just tried it with a small program and not much works.  I had to add a mo.Get(); to get anything.  The list from Computer/Manage seems to have little relationship to mangement objects.

     

    Sorry

    Sunday, September 05, 2010 4:20 PM
  • Robert,

     

    I was on Win7 64bit as well.  I imagine XP is different.  Regardless, the issue still remains how do you get the appropriate entry.  What did you mean earlier by stating that you were looking for a WPF answer?  This issue is a .net framework issue, not necesasrily WPF, Winforms, ASP, etc.

     

    Instead of going at the logical disk go at the disk drive object and see if you get what you need:

     

      void MainWindow_Loaded(object sender, RoutedEventArgs e)
      {
       ManagementObjectSearcher moSearch = new ManagementObjectSearcher ("Select * from Win32_DiskDrive where InterfaceType='USB'");
       foreach (var mo in moSearch.Get())
       {
        tbID.Text = mo["PNPDeviceID"].ToString();
        MessageBox.Show("Device Instance Id: " + tbID.Text);<br/>
        }
      }
    

     

     


    May we all make money in the sequel.
    Sunday, September 05, 2010 4:20 PM
  • Not sure if this will help but MS has a WMI Administrative Tools which includes a browser.  It may help you with your query and the correct name for the item.

    Link is:

    http://www.microsoft.com/downloads/details.aspx?familyid=6430F853-1120-48DB-8CC5-F2ABDC3ED314&displaylang=en

     

    LS

    Sunday, September 05, 2010 4:52 PM
  • Just use the 3rd party library to see how they make the job happen. Also, WPF has nothing to do with USB, since WPF is only there for the UI part of an app (and the view model, but in the end the UI). Therefore, and old link should work like a charm. Remember that most of the .NET Framework is just a wrapper around Win32 API's.


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    Tuesday, September 07, 2010 5:05 AM