Exemple #1
0
        private MTPDevice GetMTPDevice()
        {
            Device device = Metadata.Source as Device;

            if (device == null)
            {
                return(null);
            }
            MTPDevice mtpDevice = MtpDeviceManager.Instance.GetMTPDevice(device);

            if (mtpDevice != null)
            {
                mtpDevice.RootFileNode = MtpDeviceManager.Instance.GetRootFileNode(mtpDevice, null);
            }
            return(mtpDevice);
        }
Exemple #2
0
        private MTPDevice GetMTPDevice(DataPumpControllableExecutionContext context)
        {
            Device device = context.PumpDescriptor.Source as Device;

            if (device == null)
            {
                return(null);
            }
            MTPDevice mtpDevice = MtpDeviceManager.Instance.GetMTPDevice(device);

            if (mtpDevice != null)
            {
                mtpDevice.RootFileNode = MtpDeviceManager.Instance.GetRootFileNode(mtpDevice, context.Reporter);
            }
            return(mtpDevice);
        }
        public void GetDate(MTPDevice device, MTPFileNode fileNode)
        {
            IPortableDeviceContent content = GetDeviceContent(device.DeviceId);

            IPortableDeviceProperties properties;

            content.Properties(out properties);

            IPortableDeviceValues objectValues;

            properties.GetValues(fileNode.Id, null, out objectValues);

            //暂时只获取文件的创建、修改、最后访问时间
            //由于不用手机设备对MTP协议的支持程度不同,导致不同设备可能获取到的多少不一致
            fileNode.DateCreated  = GetFileDateCreatedProperty(objectValues);
            fileNode.DateModified = GetFileDateModifiedProperty(objectValues);
            fileNode.DateAuthored = GetFileDateAuthoredProperty(objectValues);
        }
        public static List <MTPDevice> GetDevices()
        {
            var devices = new List <MTPDevice>();

            string[]               deviceIds = EnumerateDevices();
            PortableDevice         portableDevice;
            IPortableDeviceContent deviceContent;

            MTPDevice tempDevice;

            if (!deviceIds.IsValid())
            {
                return(devices);
            }

            foreach (var deviceId in deviceIds)
            {
                IPortableDeviceValues deviceValues = Connect(deviceId, out portableDevice, out deviceContent);
                MTPDeviceType         deviceType   = GetDeviceType(deviceValues);
                //if (deviceType == MTPDeviceType.MediaPlayer || deviceType == MTPDeviceType.Phone)
                {
                    try
                    {
                        tempDevice                 = new MTPDevice();
                        tempDevice.DeviceId        = deviceId;
                        tempDevice.DeviceType      = deviceType;
                        tempDevice.SerialNumber    = GetSerialNumber(deviceValues);
                        tempDevice.DeviceName      = GetDeviceName(deviceValues);
                        tempDevice.FirmwareVersion = GetFirmwareVersion(deviceValues);
                        tempDevice.Manufacturer    = GetManufacturer(deviceValues);
                        tempDevice.Model           = GetModel(deviceValues);

                        devices.Add(tempDevice);
                    }
                    catch
                    { }
                }

                Disconnect(portableDevice);
            }

            return(devices);
        }
Exemple #5
0
        /// <summary>
        /// 使用特定的执行上下文执行服务。
        /// </summary>
        /// <param name="context">执行上下文。</param>
        protected override void ExecuteCore(DataPumpControllableExecutionContext context)
        {
            SourceFileItem source = context.Source;

            if (source.ItemType != SourceFileItemType.FileExtension)
            {
                throw new InvalidOperationException("Only support FileExtension");
            }
            MTPDevice mtpDevice = GetMTPDevice();

            if (mtpDevice == null)
            {
                return;
            }
            //2.解析文件类型和文件后缀名列表
            var filetype   = Regex.Match(source.Config, @"^\$(\S+),").Groups[1].Value;
            var extensions = source.Config.Substring(filetype.Length).Split(';').Select(ex => string.Format(".{0}", ex));
            //3.获取文件列表
            var fileNodes = mtpDevice.GetFiles(extensions);
            //4.拷贝文件
            String sourcePath;
            String destPath;

            foreach (var fileNode in fileNodes)
            {
                sourcePath = fileNode.GetFullPath();
                destPath   = Path.Combine(Metadata.SourceStorePath, filetype, sourcePath);
                FileHelper.CreateDirectory(destPath);
                if (MtpDeviceManager.Instance.CopyFileFromDevice(mtpDevice, fileNode, destPath))
                {
                    var copyfile = new FileInfo(Path.Combine(destPath, fileNode.Name));
                    if (copyfile.Exists)
                    {
                        //修改文件的 创建时间、最后修改时间、最后访问时间
                        MtpDeviceManager.Instance.GetDate(mtpDevice, fileNode);

                        File.SetCreationTime(copyfile.FullName, CovertMTPDateTime(fileNode.DateCreated));
                        File.SetLastWriteTime(copyfile.FullName, CovertMTPDateTime(fileNode.DateModified));
                        File.SetLastAccessTime(copyfile.FullName, CovertMTPDateTime(fileNode.DateAuthored));
                    }
                }
            }
        }
 /// <summary>
 /// 从设备复制文件到本地
 /// </summary>
 /// <param name="device"></param>
 /// <param name="file"></param>
 /// <param name="dstPath"></param>
 public bool CopyFileFromDevice(MTPDevice device, MTPFileNode file, string dstPath)
 {
     return(TransferContentFromDevice(Path.Combine(dstPath, file.Name), device.DeviceId, file.Id) == string.Empty);
 }
        public MTPFileNode GetRootFileNode(MTPDevice device, IAsyncProgress asyn)
        {
            MTPFileNode root = new MTPFileNode()
            {
                Type = MTPFileNodeType.Root, Name = "Root", Childrens = new List <MTPFileNode>(), Level = -1
            };

            IPortableDeviceContent content = GetDeviceContent(device.DeviceId);

            IPortableDeviceProperties properties;

            content.Properties(out properties);

            IPortableDeviceValues deviceValues;

            properties.GetValues("DEVICE", null, out deviceValues);

            List <string> storagesId = GetChildrenObjectIds(content, "DEVICE"); //获取存储卡设备

            //asyn.Advance(0, string.Format("获取到{0}个存储设备", storagesId.Count));

            foreach (string storageId in storagesId)
            {
                MTPFileNode deviceNode = new MTPFileNode()
                {
                    Type      = MTPFileNodeType.Device,
                    Name      = GetNameById(storageId, properties),
                    Id        = storageId,
                    Childrens = new List <MTPFileNode>(),
                    Parent    = root,
                    //Level = 0
                };
                root.Childrens.Add(deviceNode);
            }

            foreach (var parentNode in root.Childrens)
            {
                //asyn.Advance(0, string.Format(LanguageHelper.Get("LANGKEY_KaiShiHuoQuDeWenJianXiTong_00553"), parentNode.Name));

                List <string> objectsId = GetChildrenObjectIds(content, parentNode.Id);
                if (objectsId != null && objectsId.Count > 0)
                {
                    foreach (string objectId in objectsId)
                    {
                        IPortableDeviceValues objectValues;
                        properties.GetValues(objectId, null, out objectValues);
                        MTPFileNode fileNode = new MTPFileNode()
                        {
                            Type = GetFileTypeProperty(objectValues),
                            Name = GetFullNameProperty(objectValues),
                            //FileSize = GetFileSizeProperty(objectValues),
                            Id        = objectId,
                            Childrens = new List <MTPFileNode>(),
                            Parent    = parentNode,
                            //Level = parentNode.Level + 1
                        };

                        parentNode.Childrens.Add(fileNode);

                        //asyn.Advance(10.0 / root.Childrens.Count / objectsId.Count, string.Format(LanguageHelper.Get("LANGKEY_HuoQuJieDian_00554"), fileNode.Name));

                        if (fileNode.Type != MTPFileNodeType.File)
                        {
                            CreateTree(fileNode, content, properties, asyn);
                        }
                    }
                }
            }

            return(root);
        }
        /// <summary>
        /// 获取MTP设备文件系统树
        /// </summary>
        /// <param name="device"></param>
        /// <param name="asyn"></param>
        /// <returns>根节点</returns>
        public MTPFileNode GetRootFileNode(MTPDevice device, IAsyncTaskProgress asyn)
        {
            MTPFileNode root = new MTPFileNode()
            {
                Type = MTPFileNodeType.Root, Name = "Root", Childrens = new List <MTPFileNode>(), Level = -1
            };

            IPortableDeviceContent content = GetDeviceContent(device.DeviceId);

            IPortableDeviceProperties properties;

            content.Properties(out properties);

            IPortableDeviceValues deviceValues;

            properties.GetValues("DEVICE", null, out deviceValues);

            List <string> storagesId = GetChildrenObjectIds(content, "DEVICE"); //获取存储卡设备

            foreach (string storageId in storagesId)
            {
                MTPFileNode deviceNode = new MTPFileNode()
                {
                    Type      = MTPFileNodeType.Device,
                    Name      = GetNameById(storageId, properties),
                    Id        = storageId,
                    Childrens = new List <MTPFileNode>(),
                    Parent    = root,
                    //Level = 0
                };
                root.Childrens.Add(deviceNode);
            }

            foreach (var parentNode in root.Childrens)
            {
                List <string> objectsId = GetChildrenObjectIds(content, parentNode.Id);
                if (objectsId != null && objectsId.Count > 0)
                {
                    foreach (string objectId in objectsId)
                    {
                        IPortableDeviceValues objectValues;
                        properties.GetValues(objectId, null, out objectValues);
                        MTPFileNode fileNode = new MTPFileNode()
                        {
                            Type = GetFileTypeProperty(objectValues),
                            Name = GetFullNameProperty(objectValues),
                            //FileSize = GetFileSizeProperty(objectValues),
                            Id        = objectId,
                            Childrens = new List <MTPFileNode>(),
                            Parent    = parentNode,
                            //Level = parentNode.Level + 1
                        };

                        parentNode.Childrens.Add(fileNode);

                        if (fileNode.Type != MTPFileNodeType.File)
                        {
                            CreateTree(fileNode, content, properties, asyn);
                        }
                    }
                }
            }

            return(root);
        }