private void fillUsbDevicesCollection() { this.UsbDevicesCollection.Clear(); foreach (ManagementObject managementObject in GetUsbComPorts()) { UsbDeviceInfo deviceInfo = getDeviceInfo((string)managementObject.GetPropertyValue(NAME), (string)managementObject.GetPropertyValue(DEVICE_ID)); if (deviceInfo != null && !this.UsbDevicesCollection.Contains(deviceInfo.DeviceSignature)) { this.UsbDevicesCollection.Add(deviceInfo.DeviceSignature, deviceInfo); } } }
private void handleDeviceChanged(EventArrivedEventArgs e, EventType eventType) { ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"]; if (new Guid((string)instance["ClassGuid"]) != GUID_DEVCLASS_COM_PORT) { return; } string infoString = ""; foreach (var property in instance.Properties) { infoString += property.Name + " = " + property.Value + Environment.NewLine; } //Logger.logd(this, infoString); UsbDeviceInfo usbDeviceInfo = getDeviceInfo((string)instance.Properties[NAME].Value, (string)instance.Properties[DEVICE_ID].Value); if (this.UsbDevicesCollection.Contains(usbDeviceInfo.DeviceSignature)) { this.UsbDevicesCollection.Remove(usbDeviceInfo.DeviceSignature); } else { this.UsbDevicesCollection.Add(usbDeviceInfo.DeviceSignature, usbDeviceInfo); } //Logger.logd(this, "Devices after: " + eventType); foreach (UsbDeviceInfo udi in this.UsbDevicesCollection.Values) { //Logger.logd(this, udi.ToString() + Environment.NewLine); //Logger.logd(this, "End of devices list."); this.UsbDevicesChangedEvent(this, new UsbDevicesChangedEventArgs(usbDeviceInfo, eventType)); } }
public UsbDevicesChangedEventArgs(UsbDeviceInfo usbDeviceInfo, UsbDevices.EventType eventType) { this.UsbDeviceInfo = usbDeviceInfo; this.EventType = eventType; }