private void removeAttachedDevicesClick(object sender, EventArgs e) { String id; try { ManagementObjectSearcher theSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'"); foreach (ManagementObject currentObject in theSearcher.Get()) { if (currentObject["PNPDeviceID"] != null) { id = currentObject["PNPDeviceID"].ToString(); } else { MessageBox.Show("A USB device does not provide PNPDeviceID, skipping"); continue; } if (id.StartsWith("USBSTOR")) { int start = id.LastIndexOf("\\") + 1; int lentgh = id.LastIndexOf("&") - start; String uniqID = id.Substring(start, lentgh); //MessageBox.Show("USB storage uniq id: " + uniqID); try { MD5 md5 = MD5.Create(); byte[] md5buf = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(uniqID)); String idHash = ""; foreach (byte b in md5buf) { idHash += b.ToString("X2"); //MessageBox.Show(idHash); } if (USBTable.Rows.Contains(idHash)) { USBTable.Rows.Remove(USBTable.Rows.Find(idHash)); USBTable.AcceptChanges(); } } catch (Exception ex) { MessageBox.Show("RemoveAttachedDevicesClick error:" + ex.Message + " " + ex.StackTrace); } } } } catch (Exception ex) { MessageBox.Show("RemoveAttachedDevicesClick error:" + ex.Message + " " + ex.StackTrace); } }
private void getAttachedDevicesClick(object sender, EventArgs e) { try { Cursor.Current = Cursors.WaitCursor; ManagementObjectSearcher theSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'"); foreach (ManagementObject currentObject in theSearcher.Get()) { String id = ""; String pid = ""; String vid = ""; String size = "N/A"; String idHash = ""; String uniqID = ""; if (currentObject["PNPDeviceID"] != null) { id = currentObject["PNPDeviceID"].ToString(); } else { MessageBox.Show("A USB device does not provide PNPDeviceID, skipping"); continue; } if (currentObject["Size"] != null) { size = currentObject["Size"].ToString(); } if (id.StartsWith("USBSTOR")) { int start = id.LastIndexOf("\\") + 1; int lentgh = id.LastIndexOf("&") - start; uniqID = id.Substring(start, lentgh).ToUpper(); //MessageBox.Show("USB storage uniq id: " + uniqID); /* * try * { * RegistryKey enumUSBKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\USBSTOR\Enum"); * int count = (int)enumUSBKey.GetValue("Count"); * * for (int i = 0; i < count; i++) * { * String usbDeviceString = enumUSBKey.GetValue(i.ToString()).ToString().ToUpperInvariant(); * if (usbDeviceString.Contains(uniqID)) * { * int startVid = usbDeviceString.IndexOf("VID_") + 4; * int endVid = usbDeviceString.IndexOf("&", startVid); * vid = usbDeviceString.Substring(startVid, endVid - startVid); * int endPid = usbDeviceString.IndexOf("\\", endVid + 1); * pid = usbDeviceString.Substring(endVid + 5, endPid - endVid - 5); * } * } * } * catch (Exception ex) * { * MessageBox.Show("Failed to get VID and PID:" + ex.Message + " " + ex.StackTrace); * } */ /* * try * { * RegistryKey enumUSBDevKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\USB"); * String devNode = ""; * foreach (String devNodeKeyString in enumUSBDevKey.GetSubKeyNames()) * { * foreach (String devNodeInstanceString in enumUSBDevKey.OpenSubKey(devNodeKeyString).GetSubKeyNames()) * { * if (devNodeInstanceString == uniqID) * { * devNode = devNodeKeyString; * break; * } * } * } * } * catch (Exception ex) * { * MessageBox.Show("Failed to get devNodeKeyString:" + ex.Message + " " + ex.StackTrace); * } */ try { MD5 md5 = MD5.Create(); byte[] md5buf = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(uniqID)); foreach (byte b in md5buf) { idHash += b.ToString("X2"); } DataRow row = USBTable.NewRow(); row.SetField(Hash, idHash); row.SetField(Id, uniqID); //row.SetField(Pid, pid); //row.SetField(Vid, vid); row.SetField(Model, currentObject["Model"]); row.SetField(Size, size); //row.SetField(DevNode, devNode); if (!USBTable.Rows.Contains(idHash)) { USBTable.Rows.Add(row); row.AcceptChanges(); } } catch (Exception ex) { MessageBox.Show("GetAttachedDevicesClick error:" + ex.Message + " " + ex.StackTrace); } } } } catch (Exception ex) { MessageBox.Show("GetAttachedDevicesClick error:" + ex.Message + " " + ex.StackTrace); } Cursor.Current = Cursors.Arrow; }