用C#实现控制台进度条

1.实现效果如下

用C#实现控制台进度条_第1张图片

2.控制台进度条实现类  

复制代码
  1 /***********************************************************************
  2 * 文 件 名:ProgressBar.cs
  3 * CopyRight(C) 2016-2020 中国XX工程技术有限公司
  4 * 文件编号:201603230001
  5 * 创 建 人:张华斌
  6 * 创建日期:2016-03-23
  7 * 修 改 人:
  8 * 修改日期:
  9 * 描    述:控制台进度条实现类
 10 ***********************************************************************/
 11 using System;
 12 
 13 namespace ProgressBarSolution
 14 {
 15     /// 
 16     /// 进度条类型
 17     /// 
 18     public enum ProgressBarType
 19     {
 20         /// 
 21         /// 字符
 22         /// 
 23         Character,
 24         /// 
 25         /// 彩色
 26         /// 
 27         Multicolor
 28     }
 29 
 30     public class ProgressBar
 31     {
 32 
 33         /// 
 34         /// 光标的列位置。将从 0 开始从左到右对列进行编号。
 35         /// 
 36         public int Left { get; set; }
 37         /// 
 38         /// 光标的行位置。从上到下,从 0 开始为行编号。
 39         /// 
 40         public int Top  { get; set; }
 41 
 42         /// 
 43         /// 进度条宽度。
 44         /// 
 45         public int Width { get; set; }
 46         /// 
 47         /// 进度条当前值。
 48         /// 
 49         public int Value { get; set; }
 50         /// 
 51         /// 进度条类型
 52         /// 
 53         public ProgressBarType ProgressBarType { get; set; }
 54 
 55     
 56         private ConsoleColor colorBack;
 57         private ConsoleColor colorFore;
 58 
 59 
 60         public ProgressBar():this(Console.CursorLeft, Console.CursorTop)
 61         {
 62 
 63         }
 64 
 65         public ProgressBar(int left, int top, int width = 50, ProgressBarType ProgressBarType = ProgressBarType.Multicolor)
 66         {
 67             this.Left = left;
 68             this.Top = top;
 69             this.Width = width;
 70             this.ProgressBarType = ProgressBarType;
 71                        
 72             // 清空显示区域;
 73             Console.SetCursorPosition(Left, Top);
 74             for (int i = left; ++i < Console.WindowWidth;) { Console.Write(" "); }
 75 
 76             if (this.ProgressBarType == ProgressBarType.Multicolor)
 77             {
 78                 // 绘制进度条背景; 
 79                 colorBack = Console.BackgroundColor;
 80                 Console.SetCursorPosition(Left, Top);
 81                 Console.BackgroundColor = ConsoleColor.DarkCyan;
 82                 for (int i = 0; ++i <= width;) { Console.Write(" "); }
 83                 Console.BackgroundColor = colorBack;
 84             }
 85             else
 86             {
 87                 // 绘制进度条背景;    
 88                 Console.SetCursorPosition(left, top);
 89                 Console.Write("[");               
 90                 Console.SetCursorPosition(left + width-1, top);
 91                 Console.Write("]");
 92             }
 93         }
 94 
 95         public int Dispaly(int value)
 96         {
 97            return  Dispaly(value, null);
 98         }
 99 
100         public int Dispaly(int value, string msg )
101         {
102             if (this.Value != value)
103             {
104                 this.Value = value;
105 
106                 if (this.ProgressBarType == ProgressBarType.Multicolor)
107                 {
108                     // 保存背景色与前景色;
109                     colorBack = Console.BackgroundColor;
110                     colorFore = Console.ForegroundColor;
111                     // 绘制进度条进度                
112                     Console.BackgroundColor = ConsoleColor.Yellow;
113                     Console.SetCursorPosition(this.Left, this.Top);
114                     Console.Write(new string(' ', (int)Math.Round(this.Value / (100.0 / this.Width))));
115                     Console.BackgroundColor = colorBack;
116 
117                     // 更新进度百分比,原理同上.                 
118                     Console.ForegroundColor = ConsoleColor.Green;
119                     Console.SetCursorPosition(this.Left + this.Width + 1, this.Top);
120                     if (string.IsNullOrWhiteSpace(msg)) { Console.Write("{0}%", this.Value); } else { Console.Write(msg); }
121                     Console.ForegroundColor = colorFore;
122                 }
123                 else
124                 {
125                     // 绘制进度条进度      
126                     Console.SetCursorPosition(this.Left+1, this.Top);
127                     Console.Write(new string('*', (int)Math.Round(this.Value / (100.0 / (this.Width - 2)))));
128                     // 显示百分比   
129                     Console.SetCursorPosition(this.Left + this.Width + 1, this.Top);
130                     if (string.IsNullOrWhiteSpace(msg)) { Console.Write("{0}%", this.Value); } else { Console.Write(msg); }
131                 }
132             }
133             return value;
134         }
135     }
136 }
复制代码

3.GZip文件操作类

 

复制代码
 1 /***********************************************************************
 2 * 文 件 名:GZipHelper.cs
 3 * CopyRight(C) 2016-2020 中国XX工程技术有限公司
 4 * 文件编号:201603230002
 5 * 创 建 人:张华斌
 6 * 创建日期:2016-03-23
 7 * 修 改 人:
 8 * 修改日期:
 9 * 描    述:GZip文件操作类
10 ***********************************************************************/
11 using System;
12 using System.IO;
13 using System.IO.Compression;
14 
15 namespace ProgressBarSolution
16 {
17     /// 
18     /// GZip文件操作类;
19     /// 
20     public class GZipHelper
21     {
22         /// 
23         /// 压缩文件;
24         /// 
25         /// 输入文件
26         /// 进度条显示函数
27         public static void Compress(string inputFileName, Func dispalyProgress = null)
28         {
29             using (FileStream inputFileStream = File.Open(inputFileName, FileMode.Open))
30             {
31                 using (FileStream outputFileStream = new FileStream(Path.Combine(Path.GetDirectoryName(inputFileName), string.Format("{0}.7z", Path.GetFileNameWithoutExtension(inputFileName))), FileMode.Create, FileAccess.Write))
32                 {
33                     using (GZipStream gzipStream = new GZipStream(outputFileStream, CompressionMode.Compress))
34                     {
35                         byte[] buffer = new byte[1024];
36                         int count = 0;
37                         while ((count = inputFileStream.Read(buffer, 0, buffer.Length)) > 0)
38                         {
39                             gzipStream.Write(buffer, 0, count);
40                             if (dispalyProgress != null) { dispalyProgress(Convert.ToInt32((inputFileStream.Position / (inputFileStream.Length * 1.0)) * 100)); }
41                         }
42                     }
43                 }
44             }
45         }
46 
47         /// 
48         /// 解压文件
49         /// 
50         /// 输入文件
51         /// 输出文件
52         /// 进度条显示函数
53         public static void Decompress(string inputFileName, string outFileName, Func dispalyProgress = null)
54         {
55             using (FileStream inputFileStream = File.Open(inputFileName, FileMode.Open))
56             {
57                 using (FileStream outputFileStream = new FileStream(outFileName, FileMode.Create, FileAccess.Write))
58                 {
59                     using (GZipStream decompressionStream = new GZipStream(inputFileStream, CompressionMode.Decompress))
60                     {
61                         byte[] buffer = new byte[1024];
62                         int count = 0;
63                         while ((count = decompressionStream.Read(buffer, 0, buffer.Length)) > 0)
64                         {
65                             outputFileStream.Write(buffer, 0, count);                           
66                             if (dispalyProgress != null) { dispalyProgress(Convert.ToInt32((inputFileStream.Position / (inputFileStream.Length * 1.0)) * 100)); }
67                         }
68                     }
69                 }
70             }
71         }
72     }
73 }
复制代码

 

 

 

4.控制台进度条测试程序

 

复制代码
 1 /***********************************************************************
 2 * 文 件 名:Program.cs
 3 * CopyRight(C) 2016-2020 中国XX工程技术有限公司
 4 * 文件编号:201603230003
 5 * 创 建 人:张华斌
 6 * 创建日期:2016-03-23
 7 * 修 改 人:
 8 * 修改日期:
 9 * 描    述:控制台进度条测试
10 ***********************************************************************/
11 using System;
12 
13 namespace ProgressBarSolution
14 {
15     class Program
16     {
17         static void Main(string[] args)
18         {
19             try
20             {
21                 Console.WriteLine();
22                 Console.WriteLine("正在压缩文件...");
23                 ProgressBar progressBar = new ProgressBar(Console.CursorLeft, Console.CursorTop, 50, ProgressBarType.Character);
24                 GZipHelper.Compress(@"D:\Temp\book.pdf", ProgressBar.Dispaly);
25                 Console.WriteLine();
26 
27                 Console.WriteLine();
28                 Console.WriteLine("正在解压文件...");
29                 progressBar = new ProgressBar(Console.CursorLeft, Console.CursorTop, 50, ProgressBarType.Multicolor);
30                 GZipHelper.Decompress(@"D:\Temp\book.7z", @"D:\Temp\book.pdf", ProgressBar.Dispaly);
31                 Console.WriteLine();
32                
33             }
34             catch (System.ArgumentOutOfRangeException ex)
35             {
36                 Console.Beep();
37                 Console.WriteLine("进度条宽度超出可显示区域!");
38             }
39             finally
40             {
41                 Console.WriteLine();
42                 Console.WriteLine("操作完成,按任意键退出!");
43                 Console.ReadKey(true);
44             }
45         }
46     }
47 }
复制代码

 

5.下载地址

http://pan.baidu.com/s/1pKvzFjt

转载于:https://www.cnblogs.com/yachao1120/p/10268039.html

你可能感兴趣的:(用C#实现控制台进度条)