Ejemplo n.º 1
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));
                    }
                }
            }
        }