public MainWindow()
{
InitializeComponent();
StartDeviceDetection();
}
public void StartDeviceDetection()//start device detection through USB
{
ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_USBControllerDevice'"); //Querey for specific device standard
watcher.EventArrived += USBEvents; //subscribe to events it should querey for
watcher.Query = query; //Set Query
watcher.Start();//start watch
}
private void USBEvents(object sender, EventArrivedEventArgs e)//detects when device is removed from computer
{
ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];
eventType = e.NewEvent.ClassPath.ClassName;
//scanning for device arrival
if (eventType.Equals("__InstanceCreationEvent"))
{
aDeviceArrived();
}
}
private void aDeviceArrive(){
List<string> allPaths = {list of all paths with specific}
foreach(string path in allPaths){
if(path.Contains(VID) && path.Contains(PID)){
Device device = new Device();
device.path = path;
gridWithDevices.add(device);
}
}
public MainWindow()
{
InitializeComponent();
StartDeviceDetection();
}
public void StartDeviceDetection()//start device detection through USB
{
ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_USBControllerDevice'"); //Querey for specific device standard
watcher.EventArrived += USBEvents; //subscribe to events it should querey for
watcher.Query = query; //Set Query
watcher.Start();//start watch
}
private void USBEvents(object sender, EventArrivedEventArgs e)//detects when device is removed from computer
{
ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];
eventType = e.NewEvent.ClassPath.ClassName;
//scanning for device arrival
if (eventType.Equals("__InstanceCreationEvent"))
{
aDeviceArrived();
}
}
private void aDeviceArrive(){
List<string> allPaths = {list of all paths with specific}
foreach(string path in allPaths){
if(path.Contains(VID) && path.Contains(PID)){
Device device = new Device();
device.path = path;
gridWithDevices.add(device);
}
}