Afeef さんのプロフィールAfeef Ahmed Janjuaフォトブログリストその他 ツール ヘルプ
    5月9日

    Getting the USB Devices Information using WMI

    One day at work i had a unique requirement. I wanted to list the usb devices attached with the client and then get their vendor or product id to store at the backend. I started search on google and found out that their was next to nothing on this subject. I also found out that many people were interested in the solution. Somehow after reading a awful amount of msdn documentation, it came to my understanding that i have to query two different Windows Management Instrumentation (WMI) classes to achieve the goal.

    First of all i had to Enable Privilages on the ManagementScope object just to make sure that every thing works fine. Next came the QueryObject class that i used to create my first query using the “Win32_USBControllerDevice” WMI class. Using these ManagementScope and QueryObject instances i created an instance of ManagementObjectSearcher class and called its Get() method. This Get() method returned me an instance of ManagementObjectCollection class. I was interested in the Dependent property in each of the ManagementObject in this ManagementObjectCollection instance.

    The Dependent property in each ManagementObject instance contains the DeviceId of a single usb device. So i extracted the DeviceId and created a second query based on this DeviceId. This time i queried “Win32_PnPEntity” WMI class. Which is a class for plug-n-play entities attached to the system. As you may guess there are all sorts of plug-n-play devices attached with the system. That’s precisely why we need the DeviceId in the query string. I again queried the hardware using the classes discussed above and got yet another a ManagementObjectCollection instance. For each ManagementObject instance i accessed my desired properties. I was mainly interested in the following properties

    • Device Description
    • Manufacturer (if any)
    • Device VendorId or ProductId
    • DeviceId (if any)

    Following is a complete listing of the code

       1: protected void Page_Load(object sender, EventArgs e)
       2: {
       3:      ManagementScope scope = new ManagementScope("root\\CIMV2");
       4:     scope.Options.EnablePrivileges = true;
       5:     string Win32_USBControlerDevice = "Select * From Win32_USBControllerDevice";
       6:     ObjectQuery query = new ObjectQuery(Win32_USBControlerDevice);
       7:     ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
       8:     foreach (ManagementObject mgmtObj in searcher.Get())
       9:     {
      10:         string strDeviceName = mgmtObj["Dependent"].ToString();
      11:         string strQuotes = "'";
      12:         strDeviceName = strDeviceName.Replace("\"", strQuotes);
      13:         string[] arrDeviceName = strDeviceName.Split('=');
      14:         strDeviceName = arrDeviceName[1];
      15:         string Win32_PnPEntity = "Select * From Win32_PnPEntity "
      16:             + "Where DeviceID =" + strDeviceName;
      17:         ManagementObjectSearcher mySearcher = 
      18:             new ManagementObjectSearcher(Win32_PnPEntity);
      19:         foreach (ManagementObject mobj in mySearcher.Get())
      20:         {
      21:             string strDeviceID = mobj["DeviceID"].ToString();
      22:             string[] arrDeviceID = strDeviceID.Split('\\');
      23:             Response.Write("<br />");
      24:             Response.Write("<br />");
      25:             Response.Write("Device Description = " 
      26:                     + mobj["Description"].ToString());
      27:             Response.Write("<br />");
      28:             if (mobj["Manufacturer"] != null)
      29:             {
      30:                 Response.Write("Device Manufacturer = " 
      31:                     + mobj["Manufacturer"].ToString());
      32:                 Response.Write("<br />");
      33:             }
      34:             Response.Write("Device Version ID & Vendor ID = " + arrDeviceID[1]);
      35:             Response.Write("<br />");
      36:             Response.Write("Device ID = " + arrDeviceID[2].Trim('{', '}'));
      37:             Response.Write("<br />");
      38:         }
      39:     }
      40: }
    Share this post :

    コメント

    しばらくお待ちください。
    入力されたコメントは長すぎます。短くしてください。
    何も入力されていません。もう一度やり直してください。
    現在、コメントを追加できません。後でもう一度やり直してください。
    コメントと書くには、保護者 (ほごしゃ) の方の許可 (きょか) をもらってください。許可をリクエストする
    保護者 (ほごしゃ) の方が、あなたがコメントを書けないようにしています。
    現在、コメントを削除できません。後でもう一度やり直してください。
    1 日に投稿できるコメントの最大数を超えました。24 時間経過してから、もう一度やり直してください。
    あなたが他のユーザーに対して迷惑行為を行っている可能性があると確認されたため、お使いのアカウントによるコメントの投稿を無効にしています。誤って無効にされたと思われる場合は、Windows Live のサポートにお問い合わせください。
    コメントを投稿する前に、以下のセキュリティ チェックを完了してください。
    セキュリティ チェックに入力する文字は、画像に表示されている文字または音声で流れた文字と一致していなければいけません。

    コメントを投稿するには、お使いの Windows Live ID でサインインしてください (Hotmail、Messenger、または Xbox LIVE を既に使用している場合は、そのアカウントが Windows Live ID です)。サインイン


    Windows Live ID をお持ちでない場合は、アカウントを新規登録してください。

    トラックバック

    この記事のトラックバックの URL は次のとおりです。
    http://afeefahmed283.spaces.live.com/blog/cns!70306C185B03B178!403.trak
    この記事を参照しているブログ
    • なし