I am trying to create a fast and somewhat intelligent file copy algorithm (in C #, but regardless of the platform).
My goals:
- I do not want to use any specific platform code (no pin-cops or anything else)
- I would like to use multiple cores, but that seems silly because reading / writing at the same time will look slower? (correct me please if I am wrong)
- I want to track copy progress, so File.Copy is not an option
The code I came up with is nothing special, and I'm looking for ways to speed it up:
public bool Copy(string sourcePath, string destinationPath, ref long copiedSize, long totalSize, int fileNum, int fileCount, CopyProgressCallback progressCallback)
{
FileStream source = File.OpenRead(sourcePath);
FileStream dest = File.Open(destinationPath, FileMode.Create);
int size = (int)(1024 * 256);
int read = 0;
byte[] buffer = new byte[size];
try
{
while ((read = source.Read(buffer, 0, size)) != 0)
{
dest.Write(buffer, 0, read);
copiedSize += read;
progressCallback(copiedSize, totalSize, fileNum, fileCount, j);
}
return true;
}
catch
{
return false;
}
finally
{
source.Close();
dest.Close();
}
}
What I tried and failed:
- Buffer increase as you move (speed loss and caching problems with CD / DVD)
- Tried 'CopyFileEx' - pin codes slowed down copying
- 256 , .
- , -
- "progressCallback" 1 ( ) -
- /, . ,
t - .