/// <summary> /// Create the target dirextroy for an backup or to restpore /// </summary> /// <param name="sourceDirectory">Source directroy to copy</param> /// <param name="targetDirectory">Target directroy to create</param> /// <param name="worker">BackgroundWorker for count</param> /// <param name="progress">Progress element for the process</param> /// <param name="exception">Exception of the process</param> /// <param name="copyItemClass">CcopyItem to use the GetFullDiscExceptionReturnCode function</param> /// <returns>Exception level of the copy process</returns> public TaskException.ExceptionLevel TargetDirectory(DirectoryInfo sourceDirectory, DirectoryInfo targetDirectory, BackgroundWorker worker, ProgressStore progress, out Exception exception, CopyItems copyItemClass) { exception = null; try { if (!targetDirectory.Exists && !OLKI.Toolbox.DirectoryAndFile.Path.IsDrive(targetDirectory)) { targetDirectory.Create(); HandleAttributes.Direcotry.Remove(targetDirectory); } return(TaskException.ExceptionLevel.NoException); } catch (Exception ex) { exception = ex; TaskException Exception = new TaskException { Description = Properties.Stringtable._0x001D, Exception = ex, Level = copyItemClass.GetFullDiscExceptionReturnCode(exception, TaskException.ExceptionLevel.Medium), Source = sourceDirectory.FullName, Target = targetDirectory.FullName }; progress.Exception = Exception; worker.ReportProgress((int)TaskControle.TaskStep.Exception, new ProgressState(progress, true)); return(Exception.Level); } }
/// <summary> /// Remove Metadate from target file, if target already exists /// </summary> /// <param name="sourceFile">Source file to copy</param> /// <param name="targetFile">Target file to remove metadate</param> /// <param name="worker">BackgroundWorker for count</param> /// <param name="exception">Exception of the process</param> /// <returns>Exception level of the copy process</returns> private bool CopyFileRemoveMetadata(FileInfo sourceFile, FileInfo targetFile, BackgroundWorker worker, out Exception exception) { exception = null; if (!targetFile.Exists) { return(false); } if (!HandleAttributes.File.Remove(targetFile, out exception)) { TaskException Exception = new TaskException { Description = Properties.Stringtable._0x0013, Exception = exception, Level = this.GetFullDiscExceptionReturnCode(exception, TaskException.ExceptionLevel.Slight), Source = sourceFile.FullName, Target = targetFile.FullName }; this._progress.Exception = Exception; worker.ReportProgress((int)TaskControle.TaskStep.Exception, new ProgressState(this._progress, true)); return(false); } return(true); }
/// <summary> /// Create a new root directroy for creating backups /// </summary> /// <param name="targetDirectory">A string that specifice the target directory path to create</param> /// <param name="worker">BackgroundWorker for copy</param> /// <param name="e">Provides data for the BackgroundWorker</param> /// <returns>True if the directroy was created sucessfully</returns> private bool CreateRootDirectory(string targetDirectory, BackgroundWorker worker, DoWorkEventArgs e) { try { Directory.CreateDirectory(targetDirectory); return(true); } catch (Exception ex) { TaskException Exception = new TaskException { Description = Properties.Stringtable._0x001E, Exception = ex, Level = TaskException.ExceptionLevel.Critical, Source = "", Target = targetDirectory }; this._progress.Exception = Exception; worker.ReportProgress((int)TaskControle.TaskStep.Exception, new ProgressState(this._progress, true)); e.Cancel = true; worker.CancelAsync(); return(false); } }
/// <summary> /// Create an open target file to copy a file with in bufferd parts /// </summary> /// <param name="sourceFile">Source file to copy</param> /// <param name="targetFile">Target file to copy the data to</param> /// <param name="bufferSize">Buffer size for copy files with in bufferd parts</param> /// <param name="writer">File writer to write data to target file</param> /// <param name="worker">BackgroundWorker for count</param> /// <param name="exception">Exception of the process</param> /// <returns>Exception level of the copy process</returns> private bool CopyFileCreateTarget(FileInfo sourceFile, FileInfo targetFile, int bufferSize, out BinaryWriter writer, BackgroundWorker worker, out Exception exception) { exception = null; try { FileStream FileStream = new FileStream(targetFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize); writer = new BinaryWriter(FileStream); return(true); } catch (Exception ex) { writer = null; exception = ex; TaskException Exception = new TaskException { Description = Properties.Stringtable._0x0011, Exception = ex, Level = this.GetFullDiscExceptionReturnCode(exception, TaskException.ExceptionLevel.Medium), Source = sourceFile.FullName, Target = targetFile.FullName }; this._progress.Exception = Exception; worker.ReportProgress((int)TaskControle.TaskStep.Exception, new ProgressState(this._progress, true)); return(false); } }
/// <summary> /// Write data to target file, to copy a file with in bufferd parts /// </summary> /// <param name="sourceFile">Source file to copy</param> /// <param name="targetFile">Target file to copy the data to</param> /// <param name="bufferSize">Buffer size for copy files with in bufferd parts</param> /// <param name="reader">File reader to read files</param> /// <param name="writer">File writer to write data to target file</param> /// <param name="worker">BackgroundWorker for count</param> /// <param name="e">Provides data for the BackgroundWorker</param> /// <param name="exception">Exception of the process</param> /// <returns>Exception level of the copy process</returns> private bool CopyFileWriteDate(FileInfo sourceFile, FileInfo targetFile, int bufferSize, BinaryReader reader, BinaryWriter writer, BackgroundWorker worker, DoWorkEventArgs e, out Exception exception) { exception = null; try { int read = 0; byte[] FileDataPart = new byte[bufferSize]; while ((read = reader.Read(FileDataPart, 0, bufferSize)) > 0) { writer.Write(FileDataPart, 0, read); this._progress.FileBytes.ActualValue += read; this._progress.TotalBytes.ActualValue += read; worker.ReportProgress((int)TaskControle.TaskStep.Copy_Busy, new ProgressState(this._progress)); if (worker.CancellationPending) { e.Cancel = true; return(false); } } return(true); } catch (Exception ex) { exception = ex; TaskException Exception = new TaskException { Description = Properties.Stringtable._0x0012, Exception = ex, Level = this.GetFullDiscExceptionReturnCode(exception, TaskException.ExceptionLevel.Medium), Source = sourceFile.FullName, Target = targetFile.FullName }; this._progress.Exception = Exception; worker.ReportProgress((int)TaskControle.TaskStep.Exception, new ProgressState(this._progress, true)); return(false); } finally { if (writer != null) { writer.Dispose(); } if (reader != null) { reader.Dispose(); } } }
/// <summary> /// Write Metadate to target file /// </summary> /// <param name="sourceFile">Source file to copy metadata</param> /// <param name="targetFile">Target file to write metadate</param> /// <param name="worker">BackgroundWorker for count</param> /// <param name="exception">Exception of the process</param> /// <returns>Exception level of the copy process</returns> private bool CopyFileWriteMetadata(FileInfo sourceFile, FileInfo targetFile, BackgroundWorker worker, out Exception exception) { exception = null; try { targetFile.CreationTime = sourceFile.CreationTime; targetFile.LastAccessTime = sourceFile.LastAccessTime; targetFile.LastWriteTime = sourceFile.LastWriteTime; if (Properties.Settings.Default.Copy_FileAttributes && !HandleAttributes.File.Set(targetFile, sourceFile.Attributes, out exception)) { TaskException Exception = new TaskException { Description = Properties.Stringtable._0x0014, Exception = exception, Level = this.GetFullDiscExceptionReturnCode(exception, TaskException.ExceptionLevel.Slight), Source = sourceFile.FullName, Target = targetFile.FullName }; this._progress.Exception = Exception; worker.ReportProgress((int)TaskControle.TaskStep.Exception, new ProgressState(this._progress, true)); } targetFile.IsReadOnly = sourceFile.IsReadOnly; return(true); } catch (Exception ex) { exception = ex; TaskException Exception = new TaskException { Description = Properties.Stringtable._0x0014, Exception = ex, Level = this.GetFullDiscExceptionReturnCode(exception, TaskException.ExceptionLevel.Slight), Source = sourceFile.FullName, Target = targetFile.FullName }; this._progress.Exception = Exception; worker.ReportProgress((int)TaskControle.TaskStep.Exception, new ProgressState(this._progress, true)); return(false); } }
/// <summary> /// Delete the specified directory /// </summary> /// <param name="directory">File to delete</param> /// <param name="worker">BackgroundWorker for delte</param> /// <param name="e">Provides data for the BackgroundWorker</param> private void DeleteDirectory(DirectoryInfo directory, BackgroundWorker worker) { try { directory.Delete(true); } catch (Exception ex) { TaskException Exception = new TaskException { Description = Properties.Stringtable._0x0020, Exception = ex, Level = TaskException.ExceptionLevel.Slight, Source = directory.FullName, Target = "" }; this._progress.Exception = Exception; worker.ReportProgress((int)TaskControle.TaskStep.Exception, new ProgressState(this._progress, true)); } }
/// <summary> /// Delete the specified file /// </summary> /// <param name="file">File to delete</param> /// <param name="worker">BackgroundWorker for delte</param> /// <param name="e">Provides data for the BackgroundWorker</param> private void DeleteFile(FileInfo file, BackgroundWorker worker) { try { File.SetAttributes(file.FullName, FileAttributes.Normal); file.Delete(); } catch (Exception ex) { TaskException Exception = new TaskException { Description = Properties.Stringtable._0x0020, Exception = ex, Level = TaskException.ExceptionLevel.Slight, Source = file.FullName, Target = "" }; this._progress.Exception = Exception; worker.ReportProgress((int)TaskControle.TaskStep.Exception, new ProgressState(this._progress, true)); } }
/// <summary> /// Copy a file with in bufferd parts /// </summary> /// <param name="sourceFile">Source file to copy</param> /// <param name="targetFile">Target file to copy the data to</param> /// <param name="worker">BackgroundWorker for count</param> /// <param name="e">Provides data for the BackgroundWorker</param> /// <param name="exception">Exception of the process</param> /// <returns>Exception level of the copy process</returns> public bool CopyFileBufferd(FileInfo sourceFile, FileInfo targetFile, BackgroundWorker worker, DoWorkEventArgs e, out Exception exception) { BinaryReader Reader = null; BinaryWriter Writer = null; try { // Remove target file attributes this.CopyFileRemoveMetadata(sourceFile, targetFile, worker, out exception); if (!this.CopyFileOpenSource(sourceFile, targetFile, WRITE_BUFFER_SIZE, out Reader, worker, out exception)) { return(false); } ; if (!this.CopyFileCreateTarget(sourceFile, targetFile, WRITE_BUFFER_SIZE, out Writer, worker, out exception)) { return(false); } if (!this.CopyFileWriteDate(sourceFile, targetFile, WRITE_BUFFER_SIZE, Reader, Writer, worker, e, out exception)) { return(false); } if (!this.CopyFileWriteMetadata(sourceFile, targetFile, worker, out exception)) { return(false); } if (worker.CancellationPending) { e.Cancel = true; return(true); } return(true); } catch (Exception ex) { exception = ex; TaskException Exception = new TaskException { Description = Properties.Stringtable._0x000F, Exception = ex, Level = TaskException.ExceptionLevel.Critical, Source = sourceFile.FullName, Target = targetFile.FullName }; this._progress.Exception = Exception; worker.ReportProgress((int)TaskControle.TaskStep.Exception, new ProgressState(this._progress, true)); return(false); } finally { if (Writer != null) { Writer.Dispose(); } if (Reader != null) { Reader.Dispose(); } } }
/// <summary> /// Copya file from target to source /// </summary> /// <param name="sourceFile">Source file to copy</param> /// <param name="targetDirectory">Target directroy for copy the files to</param> /// <param name="worker">BackgroundWorker for count</param> /// <param name="e">Provides data for the BackgroundWorker</param> /// <param name="exception">Exception of the process</param> /// <returns>Exception level of the copy process</returns> private TaskException.ExceptionLevel CopyFile(FileInfo sourceFile, DirectoryInfo targetDirectory, BackgroundWorker worker, DoWorkEventArgs e, out Exception exception) { this._progress.FileBytes.ActualValue = 0; this._progress.FileBytes.ElemenName = sourceFile.FullName; this._progress.FileBytes.MaxValue = sourceFile.Length; worker.ReportProgress((int)TaskControle.TaskStep.Copy_Busy, new ProgressState(this._progress)); FileInfo TargetFile = new FileInfo(targetDirectory.FullName + @"\" + sourceFile.Name); try { if (TargetFile.Exists) { HandleExistingFiles.CheckResult HandleFile = HandleExistingFiles.GetOverwriteByAction(sourceFile, TargetFile, Properties.Settings.Default.Copy_FileExisitngAddTextDateFormat, this._project.Settings.Common.ExisitingFiles.HandleExistingItem, this._project.Settings.Common.ExisitingFiles.AddTextToExistingFile, false, out exception, this._mainForm); if (HandleFile.FormResult == DialogResult.Cancel) { worker.ReportProgress((int)TaskControle.TaskStep.Cancel, new ProgressState(true)); e.Cancel = true; worker.CancelAsync(); return(TaskException.ExceptionLevel.NoException); } else if (HandleFile.RememberAction) { this._project.Settings.Common.ExisitingFiles.HandleExistingItem = HandleFile.SelectedAction; this._project.Settings.Common.ExisitingFiles.AddTextToExistingFile = HandleFile.AddText; } //Handle overwrite state switch (HandleFile.OverwriteFile) { case HandleExistingFiles.ExistingFile.Exception: worker.ReportProgress((int)TaskControle.TaskStep.Copy_Busy, new ProgressState(this._progress)); return(TaskException.ExceptionLevel.Medium); case HandleExistingFiles.ExistingFile.Overwrite: case HandleExistingFiles.ExistingFile.Rename: //Nothing speceial to do break; case HandleExistingFiles.ExistingFile.Skip: this._progress.TotalBytes.ActualValue += sourceFile.Length; this._progress.TotalFiles.ActualValue++; worker.ReportProgress((int)TaskControle.TaskStep.Copy_Busy, new ProgressState(this._progress)); return(TaskException.ExceptionLevel.NoException); } } if (!this.CopyFileBufferd(sourceFile, TargetFile, worker, e, out exception)) { return(this.GetFullDiscExceptionReturnCode(exception, TaskException.ExceptionLevel.Medium)); } else { this._progress.TotalFiles.ActualValue++; } //Report Progress worker.ReportProgress((int)TaskControle.TaskStep.Copy_Busy, new ProgressState(this._progress)); return(TaskException.ExceptionLevel.NoException); } catch (Exception ex) { exception = ex; TaskException.ExceptionLevel ReturnLevel = this.GetFullDiscExceptionReturnCode(exception, TaskException.ExceptionLevel.Medium); TaskException Exception = new TaskException { Description = Properties.Stringtable._0x000E, Exception = ex, Level = ReturnLevel, Source = sourceFile.FullName, Target = TargetFile.FullName }; this._progress.Exception = Exception; worker.ReportProgress((int)TaskControle.TaskStep.Exception, new ProgressState(this._progress, true)); return(ReturnLevel); } }