Afeef さんのプロフィールAfeef Ahmed Janjuaフォトブログリストその他 ![]() | ヘルプ |
|
5月9日 Getting the USB Devices Information using WMIOne 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
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: }
トラックバックこの記事のトラックバックの URL は次のとおりです。 http://afeefahmed283.spaces.live.com/blog/cns!70306C185B03B178!403.trak この記事を参照しているブログ
|
|
|