Beispiel #1
0
        public static async Task <List <string> > MoveFiles(Dictionary <string, string> files)
        {
            var completedTaskList = new List <string>();
            var error             = false;
            var pr     = new ProgressWindow();
            var source = new CancellationTokenSource();

            pr.Closed += (o, args) => { source.CancelAfter(TimeSpan.FromMilliseconds(1)); };
            void ProgressCallback(double prof) => pr.ProgressBar.Value = prof;

            var totalSize = files.Where(file => !File.GetAttributes(file.Key).HasFlag(FileAttributes.Directory))
                            .Sum(file => new FileInfo(file.Key).Length);

            pr.Show();

            long totalRead = 0;

            const double progressSize = 100.0;

            if (totalSize <= 0)
            {
                pr.ProgressBar.Value = 100;
            }

            foreach (var item in files)
            {
                long totalReadForFile = 0;

                var from = item.Key;
                var to   = item.Value;

                if (File.GetAttributes(from).HasFlag(FileAttributes.Directory))
                {
                    Directory.CreateDirectory(to);
                    continue;
                }


                using (var outStream = new FileStream(to, FileMode.Create, FileAccess.Write, FileShare.Read)) {
                    using (var inStream = new FileStream(from, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) {
                        try {
                            var read = totalRead;
                            await CopyStream(inStream, outStream, x => {
                                totalReadForFile = x;
                                ProgressCallback(
                                    (read + totalReadForFile) /
                                    (double)totalSize *progressSize);
                            }, source.Token).ContinueWith((task) => {
                                inStream.Dispose();
                                inStream.Close();
                                File.Delete(inStream.Name);
                                completedTaskList.Add($"{from} moved {to} {DateTime.Now}");
                            }, source.Token);
                        } catch (OperationCanceledException) {
                            outStream.Dispose();
                            outStream.Close();
                            if (File.Exists(to))
                            {
                                File.Delete(to);
                            }
                            error = true;
                            pr.Close();
                            break;
                        } catch (Exception e) {
                            MessageBox.Show(e.Message);
                            error = true;
                        }
                    }
                }

                totalRead += totalReadForFile;
            }

            if (!error)
            {
                foreach (var file in files)
                {
                    if (Directory.Exists(file.Key) && File.GetAttributes(file.Key).HasFlag(FileAttributes.Directory))
                    {
                        Directory.Delete(file.Key, true);
                    }

                    break;
                }
            }

            return(completedTaskList);
        }