class CopySample : ISample
{
long _lLastSize = 0;
string s1 = @"D:\opensource_dvd12.iso";
string s2 = @"E:\Test1.Iso";
string s3 = @"E:\Test2.iso";
Thread copyThread = null;
Thread copyWatcher = null;
#region ISample Member
public string Name
{
get { return "CopySample"; }
}
public void Main(string[] args)
{
//copyThread = new Thread(new ThreadStart(CopyThread));
//copyThread.IsBackground = true;
//copyThread.Start();
//copyWatcher = new Thread(new ThreadStart(CopyWatcher));
//copyWatcher.IsBackground = true;
//copyWatcher.Start();
Stopwatch sw1 = new Stopwatch();
Stopwatch sw2 = new Stopwatch();
sw1.Start();
File.Copy(s1, s2);
sw1.Stop();
Console.WriteLine("{0}", sw1.Elapsed);
sw2.Start();
Copy(s1, s3);
sw2.Stop();
Console.WriteLine("{0}", sw2.Elapsed);
}
private void CopyThread()
{
File.Copy(s1, s3);
}
private void CopyWatcher()
{
while (copyThread.IsAlive)
{
if(File.Exists(s3))
{
FileInfo info = new FileInfo(s3);
Console.WriteLine(info.Length);
}
Thread.Sleep(250);
}
}
private void Copy(string sSourc, string sDest)
{
int iBytes = 4096;
long lCurrentPos = 0;
FileStream fs = new FileStream(sSourc, FileMode.Open, FileAccess.Read);
FileStream sw = new FileStream(sDest, FileMode.Append, FileAccess.Write);
FileInfo fileInfo = new FileInfo(sSourc);
if (iBytes > fileInfo.Length)
iBytes = (int)fileInfo.Length;
byte[] buffer = new byte[iBytes];
while (lCurrentPos < fileInfo.Length)
{
fs.Read(buffer, 0, iBytes);
sw.Write(buffer, 0, iBytes);
lCurrentPos += iBytes;
}
fs.Close();
sw.Close();
}
#endregion
}