Пример #1
0
        public void OnProgress(ProgressInfo progress, bool done, ConsoleColor color)
        {
            int x = Console.CursorLeft;
            int y = Console.CursorTop;

            Console.SetCursorPosition(x, 10);
            Console.ForegroundColor = color;
            Console.Write($"Progress: { progress.Progress}% ");
            if (done)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\tdone!");
            }

            Console.SetCursorPosition(x, y);

            Console.ResetColor();
        }
Пример #2
0
        public async Task  CopyAsync(string source, string destination)
        {
            FileStream writeStream = null;
            FileStream readStream  = null;

            try
            {
                FileInfo fileInfo   = new FileInfo(source);
                long     fileSize   = fileInfo.Length;
                long     total      = 0;
                int      bytesRead  = -1;
                int      buffLength = 1024 * 1024;
                byte[]   buff       = new byte[buffLength];
                int      invoked    = 0;

                writeStream = new FileStream(destination, FileMode.CreateNew, FileAccess.Write);
                readStream  = new FileStream(source, FileMode.Open, FileAccess.Read);
                do
                {
                    bytesRead = await readStream.ReadAsync(buff, 0, buffLength);

                    await writeStream.WriteAsync(buff, 0, bytesRead);

                    total += bytesRead;
                    ProgressInfo pi = new ProgressInfo(total, fileSize, fileInfo.Name);
                    if (Progress && ((int)pi.Progress) / 10 > invoked)
                    {
                        OnProgress(pi, false, ConsoleColor.Red);
                        //ProgressEvent?.Invoke(pi, false);
                        invoked++;
                    }
                } while (bytesRead > 0);
                writeStream.Flush();
            }
            finally
            {
                writeStream?.Close();
                readStream?.Close();
            }
            Console.WriteLine($"Finished CopyAsync: {Thread.CurrentThread.ManagedThreadId}");
        }