/// <summary> /// Copy directory or file, take full path of source and dest as parameter. /// </summary> public static void CopyFile(string source, string dest, FileCancelDelegate cancel) { using (FileStreamEx srcStream = FileEx.OpenRead(source)) { byte[] buffer = new byte[Math.Min(1024 * 1024 * 32, srcStream.Length)]; //32MB int readCount; ushort completePercent = 0; long completeCount = 0; using (FileStreamEx destStream = FileEx.Create(dest)) { while ((readCount = srcStream.Read(buffer, 0, buffer.Length)) > 0 && !IsCancelTriggered(cancel, completePercent)) { completeCount += readCount; destStream.Write(buffer, 0, readCount); completePercent = srcStream.Length == 0 ? (ushort)100 : (ushort)((float)completeCount / (float)srcStream.Length * 100.0); } destStream.Flush(); destStream.Close(); } srcStream.Close(); } }
void CopyFile(FileInfoEx item, DirectoryInfoEx destDir) { FileSystemInfoEx lookupItem = destDir[item.Name]; if (lookupItem is FileInfoEx) { if (item.Length == (lookupItem as FileInfoEx).Length) { string srcCRC = Helper.GetFileCRC(item.FullName); string destCRC = Helper.GetFileCRC(lookupItem.FullName); if (srcCRC == destCRC && item.Length == (lookupItem as FileInfoEx).Length) { return; //Same file, no copy needed. } } } OverwriteMode overwrite = OverwriteMode.Replace; if (lookupItem != null) { overwrite = AskOverwrite(item, lookupItem); } switch (overwrite) { case OverwriteMode.Replace: if (lookupItem != null) { lookupItem.Delete(); } FileCancelDelegate cancel = delegate { return(Aborted); }; IOTools.CopyFile(item.FullName, PathEx.Combine(destDir.FullName, item.Name), cancel); break; } }
public static bool IsCancelTriggered(FileCancelDelegate cancel, ushort completePercent) { return(cancel != null && cancel(completePercent)); }
public static bool IsCancelTriggered(FileCancelDelegate cancel, ushort completePercent) { return cancel != null && cancel(completePercent); }