1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LoopCopy
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine(
"Usage: LoopCopy.exe SourceFile DestFile");
return;
}
string srcName = args[0];
string destName = args[1];
FileInfo sourceFile = new FileInfo(srcName);
if (!sourceFile.Exists)
{
Console.WriteLine("Source file {0} does not exist",
srcName);
return;
}
long fileLen = sourceFile.Length;
FileInfo destFile = new FileInfo(destName);
if (destFile.Exists)
{
Console.WriteLine("Destination file {0} already exists",
destName);
return;
}
int buflen = 1024;
byte[] buf = new byte[buflen];
long totalBytesRead = 0;
double pctDone = 0;
string msg ="";
int numReads = 0;
Console.Write("Progress:");
using (FileStream sourceStream =
new FileStream(srcName, FileMode.Open))
{
using (FileStream destStream =
new FileStream(destName, FileMode.CreateNew))
{
while (true)
{
numReads++;
int bytesRead = sourceStream.Read(buf, 0, buflen);
if (bytesRead == 0) break;
destStream.Write(buf, 0, bytesRead);
totalBytesRead += bytesRead;
if (numReads % 10 == 0)
{
for (int i = 0; i < msg.Length; i++)
{
Console.Write("\\b \\b");
}
pctDone = (double)
((double)totalBytesRead / (double)fileLen);
msg = string.Format("{0}%",
(int)(pctDone * 100));
Console.Write(msg);
}
if (bytesRead < buflen) break;
}
}
}
for (int i = 0; i < msg.Length; i++)
{
Console.Write("\\b \\b");
}
Console.WriteLine("100%");
Console.WriteLine("Done");
}
}
} |