none
Identify USB drive uniquely

    Question

  • Hi,

    I need to identify all connected usb drives uniquely. when a usb drive (pen drive or external usb hard disk) is connected, i intend to identify it uniquely and store it somewhere. Next time when this same drive is plugged again, i intend to use the stored identification info to identify the drive.

    any pointers in this regard would be grately appreciated.

    thanks,
    d2
    Saturday, July 05, 2008 4:10 PM

Answers

  • You can use WMI to do this.

    Here are some articles to get you started:

    http://www.geekpedia.com/tutorial233_Getting-Disk-Drive-Information-using-WMI-and-Csharp.html

    http://bytes.com/forum/thread234006.html

    http://69.10.233.10/KB/system/DriveDetector.aspx

    If you don't want to use WMI, an alternative would perhaps be to catch the WM_DEVICECHANGE message.


    /Ruben RJJournal
    Saturday, July 05, 2008 5:03 PM
  •  
    using System;     
    using System.Management;     
    using System.Windows.Forms;     
        
    namespace WMISample     
    {     
        public class MyWMIQuery     
        {     
            public static void Main()     
            {     
                try     
                {     
                    ManagementObjectSearcher searcher =      
                        new ManagementObjectSearcher("root\\CIMV2",      
                        "SELECT * FROM Win32_USBHub");      
        
                    foreach (ManagementObject queryObj in searcher.Get())     
                    {     
                        Console.WriteLine("-----------------------------------");     
                        Console.WriteLine("Win32_USBHub instance");     
                        Console.WriteLine("-----------------------------------");     
                        Console.WriteLine("Availability: {0}", queryObj["Availability"]);    
                        Console.WriteLine("Caption: {0}", queryObj["Caption"]);  
                        Console.WriteLine("CurrentConfigValue: {0}", queryObj["CurrentConfigValue"]);    
                        Console.WriteLine("NumberOfPorts: {0}", queryObj["NumberOfPorts"]);        
                        Console.WriteLine("NumberOfConfigs: {0}", queryObj["NumberOfConfigs"]);  
                        Console.WriteLine("CurrentConfigValue: {0}", queryObj["CurrentConfigValue"]);  
                        Console.WriteLine("Status: {0}", queryObj["Status"]);  
                        Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);  
                        if(queryObj["CurrentAlternateSettings"] == null)  
                            Console.WriteLine("CurrentAlternateSettings: {0}", queryObj["CurrentAlternateSettings"]);  
                        else  
                        {  
                            Byte[] arrCurrentAlternateSettings = (Byte[])(queryObj["CurrentAlternateSettings"]);  
                            foreach (Byte arrValue in arrCurrentAlternateSettings)  
                            {  
                                Console.WriteLine("CurrentAlternateSettings: {0}", arrValue);  
                            }  
                        }  
                        Console.WriteLine("USBVersion: {0}", queryObj["USBVersion"]);  
                        Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);   
                    }     
                }     
                catch (ManagementException e)     
                {     
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);     
                }     
            }     
        }     
    }    
     

    AlexB
    Saturday, July 05, 2008 6:49 PM

All replies

  • You can use WMI to do this.

    Here are some articles to get you started:

    http://www.geekpedia.com/tutorial233_Getting-Disk-Drive-Information-using-WMI-and-Csharp.html

    http://bytes.com/forum/thread234006.html

    http://69.10.233.10/KB/system/DriveDetector.aspx

    If you don't want to use WMI, an alternative would perhaps be to catch the WM_DEVICECHANGE message.


    /Ruben RJJournal
    Saturday, July 05, 2008 5:03 PM
  •  
    using System;     
    using System.Management;     
    using System.Windows.Forms;     
        
    namespace WMISample     
    {     
        public class MyWMIQuery     
        {     
            public static void Main()     
            {     
                try     
                {     
                    ManagementObjectSearcher searcher =      
                        new ManagementObjectSearcher("root\\CIMV2",      
                        "SELECT * FROM Win32_USBHub");      
        
                    foreach (ManagementObject queryObj in searcher.Get())     
                    {     
                        Console.WriteLine("-----------------------------------");     
                        Console.WriteLine("Win32_USBHub instance");     
                        Console.WriteLine("-----------------------------------");     
                        Console.WriteLine("Availability: {0}", queryObj["Availability"]);    
                        Console.WriteLine("Caption: {0}", queryObj["Caption"]);  
                        Console.WriteLine("CurrentConfigValue: {0}", queryObj["CurrentConfigValue"]);    
                        Console.WriteLine("NumberOfPorts: {0}", queryObj["NumberOfPorts"]);        
                        Console.WriteLine("NumberOfConfigs: {0}", queryObj["NumberOfConfigs"]);  
                        Console.WriteLine("CurrentConfigValue: {0}", queryObj["CurrentConfigValue"]);  
                        Console.WriteLine("Status: {0}", queryObj["Status"]);  
                        Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);  
                        if(queryObj["CurrentAlternateSettings"] == null)  
                            Console.WriteLine("CurrentAlternateSettings: {0}", queryObj["CurrentAlternateSettings"]);  
                        else  
                        {  
                            Byte[] arrCurrentAlternateSettings = (Byte[])(queryObj["CurrentAlternateSettings"]);  
                            foreach (Byte arrValue in arrCurrentAlternateSettings)  
                            {  
                                Console.WriteLine("CurrentAlternateSettings: {0}", arrValue);  
                            }  
                        }  
                        Console.WriteLine("USBVersion: {0}", queryObj["USBVersion"]);  
                        Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);   
                    }     
                }     
                catch (ManagementException e)     
                {     
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);     
                }     
            }     
        }     
    }    
     

    AlexB
    Saturday, July 05, 2008 6:49 PM
  •  
    using System;  
    using System.Management;  
    using System.Windows.Forms;  
     
    namespace WMISample  
    {  
        public class MyWMIQuery  
        {  
            public static void Main()  
            {  
                try  
                {  
                    ManagementObjectSearcher searcher =   
                        new ManagementObjectSearcher("root\\CIMV2",   
                        "SELECT * FROM Win32_USBController");   
     
                    foreach (ManagementObject queryObj in searcher.Get())  
                    {  
                        Console.WriteLine("-----------------------------------");  
                        Console.WriteLine("Win32_USBController instance");  
                        Console.WriteLine("-----------------------------------");  
                        Console.WriteLine("Availability: {0}", queryObj["Availability"]);  
                        Console.WriteLine("Caption: {0}", queryObj["Caption"]);  
                        Console.WriteLine("ConfigManagerUserConfig: {0}", queryObj["ConfigManagerUserConfig"]);  
                        Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);  
                        Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"])  
                        Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);  
                        Console.WriteLine("ProtocolSupported: {0}", queryObj["ProtocolSupported"]);  
                        Console.WriteLine("Status: {0}", queryObj["Status"]);  
                        Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);  
                        Console.WriteLine("TimeOfLastReset: {0}", queryObj["TimeOfLastReset"]);  
     
                    }  
                }  
                catch (ManagementException e)  
                {  
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);  
                }  
            }  
        }  

    AlexB
    Saturday, July 05, 2008 6:55 PM
  •  
    using System;  
    using System.Management;  
    using System.Windows.Forms;  
     
    namespace WMISample  
    {  
        public class MyWMIQuery  
        {  
            public static void Main()  
            {  
                try  
                {  
                    ManagementObjectSearcher searcher =   
                        new ManagementObjectSearcher("root\\CIMV2",   
                        "SELECT * FROM Win32_USBControllerDevice");   
     
                    foreach (ManagementObject queryObj in searcher.Get())  
                    {  
                        Console.WriteLine("-----------------------------------");  
                        Console.WriteLine("Win32_USBControllerDevice instance");  
                        Console.WriteLine("-----------------------------------");  
                        Console.WriteLine("AccessState: {0}", queryObj["AccessState"]);  
                        Console.WriteLine("Antecedent: {0}", queryObj["Antecedent"]);  
                        Console.WriteLine("Dependent: {0}", queryObj["Dependent"]);  
                        Console.WriteLine("NegotiatedDataWidth: {0}", queryObj["NegotiatedDataWidth"]);  
                        Console.WriteLine("NegotiatedSpeed: {0}", queryObj["NegotiatedSpeed"]);  
                        Console.WriteLine("NumberOfHardResets: {0}", queryObj["NumberOfHardResets"]);  
                        Console.WriteLine("NumberOfSoftResets: {0}", queryObj["NumberOfSoftResets"]);  
                    }  
                }  
                catch (ManagementException e)  
                {  
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);  
                      
                }  
            }  
        }  

    AlexB
    Saturday, July 05, 2008 6:59 PM