/// <summary> /// This is the method that is responsible for notifying /// receivers that the event occurred /// </summary> /// <param name="e">CopyProgressEventArgs eventargs</param> protected virtual void OnCopyProgress(CopyProgressEventArgs e) { if (this.copyProgressEventHandlerDelegate != null) { this.copyProgressEventHandlerDelegate(this, e); } }
/// <summary> /// Handles the CopyProgress event of the fileSystem control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="CopyProgressEventArgs"/> instance containing the event data.</param> private void FileSystem_CopyProgress(object sender, CopyProgressEventArgs e) { this.progressCurrent = (int)e.CopiedBytes / 1024; this.progressMax = (int)e.TotalBytes / 1024; this.eta = e.Eta.ToString(); }
/// <summary> /// Copies the sourceFile to the outFile /// </summary> /// <param name="sourceFile">Source file to be copied</param> /// <param name="outFile">File copy destination</param> /// <returns> /// A bool value that indicate a successful copy finished. /// </returns> public bool CopyFile(string sourceFile, string outFile) { var fi = new FileInfo(sourceFile); long totalBytes = fi.Length; bool success = true; if (totalBytes == 0) { // no file data File.Create(outFile).Close(); } else { var readStream = new FileStream(sourceFile, FileMode.Open); var writeStream = new FileStream(outFile, FileMode.CreateNew); int readBytes = 1; DateTime startTime = DateTime.Now; long totalCopiedBytes = 0; var buffer = new byte[this.bufferSize]; while (readBytes > 0) { readBytes = readStream.Read(buffer, 0, this.bufferSize); totalCopiedBytes += readBytes; writeStream.Write(buffer, 0, readBytes); double m = DateTime.Now.Subtract(startTime).TotalMilliseconds; double speed = totalCopiedBytes / m; double eta = (totalBytes - totalCopiedBytes) / speed; var evt = new CopyProgressEventArgs( (decimal)totalCopiedBytes / totalBytes, totalCopiedBytes, totalBytes, eta, m); this.OnCopyProgress(evt); if (evt.Cancel) { success = false; break; } } writeStream.Close(); readStream.Close(); } // If everthing is ok copy file attributes to the newly created file. if (success) { File.SetCreationTime(outFile, File.GetCreationTime(sourceFile)); File.SetLastWriteTime(outFile, File.GetLastWriteTime(sourceFile)); File.SetAttributes(outFile, File.GetAttributes(sourceFile)); } else { if (File.Exists(outFile)) { File.Delete(outFile); } } this.OnFileCopyCompleted(new FileCopyCompletedEventArgs(success)); return success; }
/// <summary> /// Copies the sourceFile to the outFile /// </summary> /// <param name="sourceFile">Source file to be copied</param> /// <param name="outFile">File copy destination</param> /// <returns> /// A bool value that indicate a successful copy finished. /// </returns> public bool CopyFile(string sourceFile, string outFile) { var fi = new FileInfo(sourceFile); long totalBytes = fi.Length; bool success = true; if (totalBytes == 0) { // no file data File.Create(outFile).Close(); } else { var readStream = new FileStream(sourceFile, FileMode.Open); var writeStream = new FileStream(outFile, FileMode.CreateNew); int readBytes = 1; DateTime startTime = DateTime.Now; long totalCopiedBytes = 0; var buffer = new byte[this.bufferSize]; while (readBytes > 0) { readBytes = readStream.Read(buffer, 0, this.bufferSize); totalCopiedBytes += readBytes; writeStream.Write(buffer, 0, readBytes); double m = DateTime.Now.Subtract(startTime).TotalMilliseconds; double speed = totalCopiedBytes / m; double eta = (totalBytes - totalCopiedBytes) / speed; var evt = new CopyProgressEventArgs( (decimal)totalCopiedBytes / totalBytes, totalCopiedBytes, totalBytes, eta, m); this.OnCopyProgress(evt); if (evt.Cancel) { success = false; break; } } writeStream.Close(); readStream.Close(); } // If everthing is ok copy file attributes to the newly created file. if (success) { File.SetCreationTime(outFile, File.GetCreationTime(sourceFile)); File.SetLastWriteTime(outFile, File.GetLastWriteTime(sourceFile)); File.SetAttributes(outFile, File.GetAttributes(sourceFile)); } else { if (File.Exists(outFile)) { File.Delete(outFile); } } this.OnFileCopyCompleted(new FileCopyCompletedEventArgs(success)); return(success); }