none
Extracting serial number, vendor id and product id from USB pendrive using C# RRS feed

  • Question

  • Hey guys, I am working on this small project where I have to extract the 3 parameters from the USB device (serial number, vendor id,product id) using c#and write them to the text files one by one.

    I could not figure in what format the data is retrieved from the WMI and so not able to format it the way it is required to be in the output text file. Can anyone please elaborate as to how the extraction is done and how to convert it into user readable format.

    A piece of code would be much appreciated.. 

    Thursday, August 30, 2012 8:13 PM

Answers

  • Hi Alanckar,

    Welcome to the MSDN Forum.

    This code produces readable result:

    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_USBDevice"); 
    
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Win32_USBDevice instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Caption: {0}", queryObj["Caption"]);
                        Console.WriteLine("Description: {0}", queryObj["Description"]);
                        Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
                        Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
                        Console.WriteLine("Name: {0}", queryObj["Name"]);
                        Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
                }
            }
        }
    }

    I hope this will be helpful.


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 31, 2012 6:46 AM
    Moderator

All replies

  • Hi Alanckar,

    Welcome to the MSDN Forum.

    This code produces readable result:

    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_USBDevice"); 
    
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Win32_USBDevice instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Caption: {0}", queryObj["Caption"]);
                        Console.WriteLine("Description: {0}", queryObj["Description"]);
                        Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
                        Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
                        Console.WriteLine("Name: {0}", queryObj["Name"]);
                        Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
                }
            }
        }
    }

    I hope this will be helpful.


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 31, 2012 6:46 AM
    Moderator
  • Hey Mike, I tried the code but it keeps throwing the Invalid class exception. I have added the necessary assembly references. Could you please help with this?

    Sunday, September 2, 2012 8:49 PM
  • Hi Alanckar,

    The above code is generated by WMI Code creator: http://sdrv.ms/PZKlKu

    And you can also try this tool:

    http://www.microsoft.com/en-us/download/details.aspx?id=24045 

    Would you like to download this tool and try to query bu this tool first?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, September 3, 2012 1:37 AM
    Moderator
  • Hi Just change:

    ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_USBDevice");

    to

    ManagementObjectSearcher searcher =

    new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");

    this will get rid of the error

    Richard Aué


    • Edited by richard5254 Wednesday, February 19, 2014 2:34 PM
    Wednesday, February 19, 2014 2:33 PM