Exemple #1
0
        private void SetClipboard(FileMoveMode mode)
        {
            ObjectListView listView = this.GetCurrentListView();

            this.ClipBoard = new FileClipBoard()
            {
                Mode = mode, SourceListView = listView, SelectedItems = listView.SelectedObjects.OfType <MyFileSystemInfo>()
            };

            if (this.ClipboardChange != null)
            {
                this.ClipboardChange(this, this.ClipBoard);
            }
        }
Exemple #2
0
        private void MoveObjects(ObjectListView sourceListView, ObjectListView targetListView, IEnumerable <MyFileSystemInfo> sources, MyFileSystemInfo target, FileMoveMode mode = FileMoveMode.None)
        {
            foreach (MyFileSystemInfo x in sources)
            {
                bool isCopy = mode == FileMoveMode.Copy || (mode == FileMoveMode.Drag && !FileHelper.IsSameDrive(x.FullName, target.FullName));

                if (!isCopy)
                {
                    if (!(sourceListView is TreeListView))
                    {
                        sourceListView.RemoveObject(x);
                    }

                    x.Parent.Children.Remove(x);
                }

                if (!(targetListView is TreeListView))
                {
                    if (!targetListView.Objects.OfType <MyFileSystemInfo>().Any(item => item.Name == x.Name))
                    {
                        targetListView.AddObject(x);
                    }
                }

                x.Parent = target;

                if (!target.Children.Any(item => item.Name == x.Name))
                {
                    target.Children.Add(x);
                }
            }
        }
Exemple #3
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);
            }
        }