你好
有没有人有可行的示例代码来复制文件并支持在源断开连接的情况下恢复文件?
在此示例中,我正在复制视频文件.如果源已断开连接,即USB已拔出,我如何支持再次恢复它们?我已经在*上尝试了一些代码,但是恢复后,视频文件似乎已损坏. FileStream是视频传输/恢复的最佳解决方案吗?
欢迎其他任何指针或技巧.
private void CreateNewCopyTo(FileStream source, FileStream dest) {
int size = (source.CanSeek) ? Math.Min((int)(source.Length - source.Position), 0x2000) : 0x2000;
byte[] buffer = new byte[size];
int read;
long fileLength = source.Length;
while ((read = source.Read(buffer, 0, buffer.Length)) > 0) {
dest.Write(buffer, 0, read);
dest.Close();
dest = new FileStream(dest.Name, FileMode.Append, FileAccess.Write);
}
dest.Close();
}
private void ResumeCopyTo(FileStream source, FileStream dest) {
int size = (source.CanSeek) ? Math.Min((int)(source.Length - source.Position), 0x2000) : 0x2000;
byte[] buffer = new byte[size];
long TempPos = source.Position;
while (true) {
int read = source.Read(buffer, 0, buffer.Length);
if (read <= 0)
return;
dest.Write(buffer, 0, read);
dest.Close();
dest = new FileStream(dest.Name, FileMode.Append, FileAccess.Write);
}
}
解决方法:
尝试插入source.Seek(dest.Length,SeekOrigin.Begin);作为ResumeCopyTo方法的第一行.