none
classifying USB parameters extraction method according to device description or any other property. RRS feed

  • Question

  • I'v succeeded in extraction of USB parameters but i want the parameters of the connected or precisely the mass storage devices,irrespective of other inbuilt USB hub..

    MY Code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.Management;
    using System.IO;

    namespace usbpararetrieval;
    {
        static class Program
        {
            static void Main()
            {
                //writing to text file d:\\.txt
                TextWriter tw = new StreamWriter("d:\\usbpararetrieve.txt");

                ManagementScope scope = new ManagementScope("root\\CIMV2");
                scope.Options.EnablePrivileges = true;
                //retrieving Device id first
                string Win32_USBControllerDevice = "SELECT * FROM Win32_USBHub WHERE Description = 'USB Mass Storage Device'";
                ObjectQuery query = new ObjectQuery(Win32_USBControllerDevice);
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

                //Searching other parameters using Device id
                foreach (ManagementObject mgmtObj in searcher.Get())
                {
                    //DEPENDENT attribute from Win32_USBControllerDevice
                    string strDeviceName = mgmtObj["Description"].ToString();
                    string strQuotes = "'";
                    strDeviceName = strDeviceName.Replace("\"", strQuotes);
                    string[] arrDeviceName = strDeviceName.Split('=');
                    strDeviceName = arrDeviceName[0];
                    string Win32_PnPEntity = "Select * From Win32_PnPEntity "
                        + "Where Description =" + strDeviceName;
                    ManagementObjectSearcher mySearcher =
                        new ManagementObjectSearcher(Win32_PnPEntity);
                    foreach (ManagementObject mobj in mySearcher.Get())
                    {
                        string strDeviceID = mobj["Description"].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("Serial Number : " + arrDeviceID[2].Trim('{', '}'));
                        tw.WriteLine();



                    }


                }
                // close the stream
                tw.Close();
            }

        }

    The underlined section is where i'm trying all kinds of queries to get those device specific parameters.Also I'v used Win32_PnPEntity,will that be helpful?

    or should I try some other dll's.

    need an urgent solution.

    Please...

    Friday, September 14, 2012 9:36 AM

Answers

All replies