使用C#访问手机文件夹和读/写

时间:2015-02-25 18:29:18

标签: c# iphone storage

很抱歉,如果有人只是发布了答案的链接,但我真的很难谷歌这个。

在插入手机然后访问存储设备时,我无法弄清楚如何检测手机。前者只是我不知道从哪里开始寻找,第二个让我感到困惑,因为我习惯用驱动器号来访问存储空间。我使用的手机(iPhone 4S和一些不同的三星galaxy和笔记)没有驱动器号。那我从哪里开始呢?

我使用C#,但我对dllimports等感到很满意。

1 个答案:

答案 0 :(得分:0)

我现在已经从我在上面的评论中链接的线程中完成了这段代码,很容易投入到程序中。

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);
            //}
        }
    }
}

返回信息如下:

 --- 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 =