Retrieving USB pendrive Vendor id, Product id and Serial Number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alanckar
    New Member
    • Sep 2012
    • 2

    Retrieving USB pendrive Vendor id, Product id and Serial Number

    I am trying to retrieve the parameters from externally connected pendrive. I have been using WMI to achieve this but not able to separate the pendrive's parameters from the other USB devices (such as USBcamera,USBHu b etc) Has anybody done it before?
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Alanckar

    Please post the code you've been using as someone here might catch where to add the parsing.
    When posting the code, select the code block and then click on the <CODE/> button in the formatting bar.

    Comment

    • alanckar
      New Member
      • Sep 2012
      • 2

      #3
      This is the code which gives details of all devices connceted via USB. Not able it modify the query for only externally connected PenDrive.

      Code:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Windows.Forms;
      using System.Management;
      using System.IO;
      
      namespace MyWMITry
      {
          static class Program
          {
             static void Main()
            {
              //writing to text file d:\\wordpressmod1.txt
              TextWriter tw = new StreamWriter("d:\\MyWMITry.txt");
               
              ManagementScope scope = new ManagementScope("root\\CIMV2");
              scope.Options.EnablePrivileges = true;
              string Win32_USBControlerDevice = "Select * From Win32_USBControllerDevice";
              ObjectQuery query = new ObjectQuery(Win32_USBControlerDevice);
              ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
              foreach (ManagementObject mgmtObj in searcher.Get())
              {
                 string strDeviceName = mgmtObj["Dependent"].ToString();
                 string strQuotes = "'";
                 strDeviceName = strDeviceName.Replace("\"", strQuotes);
                 string[] arrDeviceName = strDeviceName.Split('=');
                 strDeviceName = arrDeviceName[1];
                 string Win32_PnPEntity = "Select * From Win32_PnPEntity "
                     + "Where DeviceID =" + strDeviceName;
                 ManagementObjectSearcher mySearcher = 
                     new ManagementObjectSearcher(Win32_PnPEntity);
                 foreach (ManagementObject mobj in mySearcher.Get())
                 {
                     string strDeviceID = mobj["DeviceID"].ToString();
                     string[] arrDeviceID = strDeviceID.Split('\\');
                     tw.WriteLine("Device Description : " 
                             + mobj["Description"].ToString());
                     if (mobj["Manufacturer"] != null)
                     {
                         tw.WriteLine("Device Manufacturer : " 
                             + mobj["Manufacturer"].ToString());
                     }
                      tw.WriteLine("Device Version ID & Vendor ID : " + arrDeviceID[1]);
                      tw.WriteLine("Device ID : " + arrDeviceID[2].Trim('{', '}'));
                      tw.WriteLine();   
                 }           
             }
              // close the stream
              tw.Close();
                  }
              }
          }

      Comment

      Working...