0

I have a problem detecting portable devices, specifically samsung phone or iphone. I need to develop a program that will start once the detection of a portable plug-in and stop, once plugged out.

I've been trying this code, but only works with usb devices with storage, and not on portable device.

private void DeviceInsertedEvent(object sender, EventArrivedEventArgs e)
{
    ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];
    foreach (var property in instance.Properties)
    {
        Console.WriteLine(property.Name + " = " + property.Value);
    }
}

void DeviceRemovedEvent(object sender, EventArrivedEventArgs e)
{
    ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];
    foreach (var property in instance.Properties)
    {
        Console.WriteLine(property.Name + " = " + property.Value);
    }
}            

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    WqlEventQuery insertQuery = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBHub'");

    ManagementEventWatcher insertWatcher = new ManagementEventWatcher(insertQuery);
    insertWatcher.EventArrived += new EventArrivedEventHandler(DeviceInsertedEvent);
    insertWatcher.Start();

    WqlEventQuery removeQuery = new WqlEventQuery("SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBHub'");
    ManagementEventWatcher removeWatcher = new ManagementEventWatcher(removeQuery);
    removeWatcher.EventArrived += new EventArrivedEventHandler(DeviceRemovedEvent);
    removeWatcher.Start();

    // Do something while waiting for events
    System.Threading.Thread.Sleep(20000000);
}

please help. thanks

Ken Zhang
  • 21
  • 7
  • 1
    Off topic, but what do you hope to achieve by `Sleep`ing for 5.5 hours? – Sayse Jun 18 '14 at 06:53
  • sorry about the sleep part. i will remove it. i just need a way to detect the insert and remove of the portable device. – Ken Zhang Jun 18 '14 at 06:55
  • Does [this answer](http://stackoverflow.com/a/6003915/1324033) help at all? (from one of the related questions on the right), It may be worth noting if you only intend to look for devices via usb, (i.e not bluetooth) – Sayse Jun 18 '14 at 06:59
  • What i really need to detect is the plug in of a Samsung phone/ IPhone – Ken Zhang Jun 18 '14 at 07:19
  • Which Samsung phone? Android or WP? After pluggin it in, is it visible as a device in explorer or anywhere in windows? The WM_DEVICECHANGE message ought to help. In the post Sayse links to are many interesting links. (Not the accepted answer, though, as ever so often..) – TaW Jun 18 '14 at 10:14

1 Answers1

1

Have you tried this:

System.IO.DriveInfo [] drives = System.IO.DriveInfo.GetDrives ();
foreach (System.IO.DriveInfo drive in drives)
{
  if (drive.DriveType == DriveType.Removable)
  {
    Console.WriteLine ("Found removable drive {0}", drive.Name);
  }
}

For More about DriveInfo

jiten
  • 5,128
  • 4
  • 44
  • 73