帶有進度顯示的文件拷貝模塊

using System;
using System.IO;

namespace csmyCopy
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			FileStream fin, fout;
			Int64 len;
			Byte[] arrBuf = new byte[1428];

			Console.WriteLine ("Debug::Args->" +args.Length.ToString());

			if (args.Length != 2) {
				Console.WriteLine ("Usage: myCopy ");
				return;
			}
			try {
				fin = new FileStream (args[0], FileMode.Open);
			} catch (Exception ex) {
				Console.WriteLine ("Crit::Retn->1,Evnt->Fail to open src");
				Console.WriteLine ("Crit::Msg->" + ex.Message);
				return;
			}
			try {
				fout = new FileStream (args[1], FileMode.Create);
			} catch (Exception ex) {
				Console.WriteLine ("Crit::Retn->2,Evnt->Fail to open dst");
				Console.WriteLine ("Crit::Msg->" + ex.Message);
				return;
			}

			len = fin.Length;
			Console.Write ("Info::Leng->");
			Console.Write (len);
			Console.WriteLine ();

			try {
				while (fin.Position < len) {
					fin.Read (arrBuf, 0, 1428);
					fout.Write (arrBuf, 0, 1428);
					if (fin.Position % 131072 == 0) {
						Console.Write ("Info::Curr->");
						Console.Write (fin.Position);
						Console.Write (",Perc->");
						Console.Write (fin.Position * 100 / len);
						Console.WriteLine ("%");
					}
				}
			} catch (Exception ex) {
				Console.WriteLine ("Crit::Retn->3,Evnt->Fail to copy");
				Console.WriteLine ("Crit::Msg->" + ex.Message);
			}


			fin.Close();
			fout.Close();
			Console.WriteLine ("Info::Retn->0,Evnt->Finished");
		}
	}
}

運行結果:
csmyCopy mono.2 mono.3

D:\>csmyCopy mono.2 mono.3
Debug::Args->2
Info::Leng->   704704512
Info::Curr->     7047180,Perc->  1%
Info::Curr->    14094360,Perc->  2%
Info::Curr->    21141540,Perc->  3%
Info::Curr->    28188720,Perc->  4%
Info::Curr->    35235900,Perc->  5%
Info::Curr->    42283080,Perc->  6%
Info::Curr->    49330260,Perc->  7%
...
Info::Curr->   669470676,Perc-> 95%
Info::Curr->   676516428,Perc-> 96%
Info::Curr->   683563608,Perc-> 97%
Info::Curr->   690610788,Perc-> 98%
Info::Curr->   697657968,Perc-> 99%
Info::Curr->   704704512,Perc->100%
Info::Retn->0,Evnt->Finished
D:\>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章