コード例 #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;
            }
        }