コード例 #1
0
ファイル: CopyFileTask.cs プロジェクト: BillSon68/Sync.Net
        public void Execute()
        {
            if (!_file.Exists)
            {
                return;
            }

            WaitForFileToBeReady(_file);

            if (!_targetDirectory.Exists)
            {
                _targetDirectory.Create();
            }

            var targetFile = _targetDirectory.GetFile(_file.Name);

            if (!targetFile.Exists || _file.ModifiedDate >= targetFile.ModifiedDate)
            {
                if (!targetFile.Exists)
                {
                    targetFile.Create();
                }

                using (var stream = _file.GetStream())
                {
                    var totalBytes = stream.Length;
                    using (var destination = targetFile.GetStream())
                    {
                        long   bufferSize = 10 * 1024 * 1024;
                        byte[] buffer     = new byte[bufferSize];
                        int    progress   = 0;
                        int    read;
                        while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            destination.Write(buffer, 0, read);
                            destination.Flush();
                            progress += read;
                            OnTaskProgress(new TaskProgressEventArgs(totalBytes, progress));
                        }
                    }
                }

                targetFile.ModifiedDate = _file.ModifiedDate;
            }
        }
コード例 #2
0
        private List <IFileObject> GetFilesToCopy(IDirectoryObject source, IDirectoryObject target)
        {
            var filesToUpload = new List <IFileObject>();
            var sourceFiles   = source.GetFiles();

            foreach (var sourceFile in sourceFiles)
            {
                var targetFile = target.GetFile(sourceFile.Name);
                if (!targetFile.Exists || sourceFile.ModifiedDate >= targetFile.ModifiedDate)
                {
                    filesToUpload.Add(sourceFile);
                }
            }

            var subDirectories = source.GetDirectories();

            foreach (var sourceSubDirectory in subDirectories)
            {
                var targetSubDirectory = target.GetDirectory(sourceSubDirectory.Name);
                filesToUpload.AddRange(GetFilesToCopy(sourceSubDirectory, targetSubDirectory));
            }

            return(filesToUpload);
        }