-1

Sorry if someone just posts a link to the answer, but I was really struggling to google this.

I can't figure out how to detect mobile phones when they're plugged in and then access their storage. The former is just something I don't know where to start looking and the second befuddles me a little because I'm used to accessing storage with a drive letter. The phones I've used (iPhone 4S and a few different Samsung galaxy's and notes) don't have a drive letter. So where would I start?

I use C# but I'm comfortable with the dllimports etc.

  • Welcome to Stack Overflow. You have posed an interesting question, but it is far too broad for a suitable answer in this format. I suggest that you post code, errors that you've received and try to take it one step at a time. This question reads like "code this thing for me" and we dont do that here. – crthompson Feb 25 '15 at 18:31
  • I found the enumeration of USB devices, eventually (I do hate having no google-fu at all, maybe I should use Bing...) at this [thread](http://stackoverflow.com/a/3331509). I still have no idea where to start accessing a file system without the drive letter though! – DanFraserUK Feb 25 '15 at 20:00

1 Answers1

0

I've now done this code from the thread I linked in a comment above, nice and easy to throw into a program.

using System;
using System.Management;
using System.Threading;

namespace USBDeviceTester
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread myThread = new Thread(new ThreadStart(ThreadWorker));
            myThread.Start();
        }

        public static void ThreadWorker()
        {
            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);
        }

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

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

Which returns information as so:

 --- DEVICE REMOVED ---


 --- DEVICE INSERTED ---

Availability = 
Caption = Apple Mobile Device USB Driver
ClassCode = 
ConfigManagerErrorCode = 0
ConfigManagerUserConfig = False
CreationClassName = Win32_USBHub
CurrentAlternateSettings = 
CurrentConfigValue = 
Description = Apple Mobile Device USB Driver
DeviceID = USB\VID_05AC&PID_12A0\3ABFD2ED02E3982B5F4455FD684716A6D4958A74
ErrorCleared = 
ErrorDescription = 
GangSwitched = 
InstallDate = 
LastErrorCode = 
Name = Apple Mobile Device USB Driver
NumberOfConfigs = 
NumberOfPorts = 
PNPDeviceID = USB\VID_05AC&PID_12A0\3ABFD2ED02E3982B5F4455FD684716A6D4958A74
PowerManagementCapabilities = 
PowerManagementSupported = 
ProtocolCode = 
Status = OK
StatusInfo = 
SubclassCode = 
SystemCreationClassName = Win32_ComputerSystem
SystemName = MyComputerName
USBVersion =