コード例 #1
0
        public static async Task MoveFile(string source, string target, Action <TransferProgress> progress, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (IsSameDrive(source, target))
            {
                await FileTransferManager.MoveWithProgressAsync(source, target, progress, cancellationToken);
            }
            else
            {
                await CopyFile(source, target, progress, cancellationToken);

                File.Delete(source);
            }
        }
コード例 #2
0
        private async void MoveFiles(ObjectListView sourceListView, ObjectListView targetListView, IEnumerable <MyFileSystemInfo> sources, MyFileSystemInfo target, FileMoveMode mode = FileMoveMode.None)
        {
            if (mode == FileMoveMode.None)
            {
                return;
            }

            long totalSize        = 0;
            int  fileCount        = 0;
            long totalTransferred = 0;

            foreach (MyFileSystemInfo source in sources)
            {
                if (source.IsDirectory)
                {
                    fileCount += FileHelper.GetFolderFileCount(source.AsDirectory);
                    totalSize += FileHelper.GetFolderSize(source.AsDirectory);
                }
                else
                {
                    totalSize += source.AsFile.Length;
                    fileCount++;
                }
            }

            this.cancellationTokenSource = new CancellationTokenSource();

            FileCopyInfo info = new FileCopyInfo()
            {
                TotalSize = totalSize, FileCount = fileCount
            };

            Dictionary <string, TransferProgress> fileTransferred = new Dictionary <string, TransferProgress>();

            Action <TransferProgress> fileCopyProgress = (progress) =>
            {
                if (this.isCanceled)
                {
                    return;
                }

                if (!fileTransferred.ContainsKey(progress.SourcePath))
                {
                    fileTransferred.Add(progress.SourcePath, progress);
                }
                else
                {
                    fileTransferred[progress.SourcePath] = progress;
                }

                totalTransferred   = fileTransferred.Sum(item => item.Value.Transferred);
                info.FinishedCount = fileTransferred.Where(item => item.Value.Transferred == item.Value.Total).Count();

                info.TotalTransferred   = totalTransferred;
                info.CurrentSize        = progress.StreamSize;
                info.CurrentTransferred = progress.Transferred;
                info.CurrentPercent     = progress.Fraction;
                info.TotalPercent       = totalTransferred / (totalSize * 1.0);

                this.Feedback($"Progress:({info.FinishedCount}/{info.FileCount}) {FileHelper.FormatFileSize(info.TotalTransferred)}/{FileHelper.FormatFileSize(info.TotalSize)}, {info.TotalPercent.ToString("P")}");
            };

            try
            {
                this.isCanceled = false;
                this.isMoving   = true;

                foreach (MyFileSystemInfo source in sources)
                {
                    if (this.isCanceled)
                    {
                        break;
                    }

                    if (source.IsDirectory)
                    {
                        DirectoryInfo sourceFolder = source.AsDirectory;

                        string targetFolder = Path.Combine(target.FullName, sourceFolder.Name);

                        if (FileHelper.IsSameDrive(source.AsDirectory.FullName, targetFolder) && (mode == FileMoveMode.Cut || mode == FileMoveMode.Drag))
                        {
                            await FileTransferManager.MoveWithProgressAsync(source.AsDirectory.FullName, targetFolder, fileCopyProgress, this.cancellationTokenSource.Token);
                        }
                        else
                        {
                            await FileHelper.CopyFolder(source.AsDirectory.FullName, targetFolder, true, fileCopyProgress, this.cancellationTokenSource.Token);

                            if (mode == FileMoveMode.Cut)
                            {
                                source.AsDirectory.Delete(true);
                            }
                        }
                    }
                    else
                    {
                        FileInfo sourceFile = source.AsFile;

                        string targetFilePath = Path.Combine(target.FullName, sourceFile.Name);

                        if (FileHelper.IsSameDrive(sourceFile.DirectoryName, target.FullName) && (mode == FileMoveMode.Cut || mode == FileMoveMode.Drag))
                        {
                            await FileHelper.MoveFile(sourceFile.FullName, targetFilePath, fileCopyProgress, this.cancellationTokenSource.Token);
                        }
                        else
                        {
                            await FileHelper.CopyFile(sourceFile.FullName, targetFilePath, fileCopyProgress, this.cancellationTokenSource.Token);

                            if (mode == FileMoveMode.Cut)
                            {
                                sourceFile.Delete();
                            }
                        }
                    }
                }

                this.MoveObjects(sourceListView, targetListView, sources, target, mode);
            }
            catch (Exception ex)
            {
                this.Feedback(ex.Message, true);
            }
            finally
            {
                this.isMoving = false;

                if (mode != FileMoveMode.Copy)
                {
                    this.RefreshListView(this.navigator);
                }

                this.RefreshListView(this.lvFiles);
            }
        }