您的位置:首页 > 其它

带有进度显示的文件拷贝模块

2015-09-08 20:32 211 查看
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:\>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: