public static void Create(String path, NameConflictOption aNameConflictOption) { if (File.Exists(path)) { switch (aNameConflictOption) { case NameConflictOption.Cancel: case NameConflictOption.Skip: return; case NameConflictOption.Overwrite: FileExt.Delete(path); break; case NameConflictOption.RenameExisting: FileExt.Move(path, FileExt.MakeUnique(path)); break; case NameConflictOption.RenameNew: throw new InvalidDataException("Can't rename a file we are trying to create"); default: break; } } DirectoryExt.Create(Path.GetDirectoryName(path)); FileStream stream = File.Create(path); stream.Close(); }
/// <summary> /// Copies a directory. /// </summary> /// <param name="aSource">The source location.</param> /// <param name="aDestination">The destination location.</param> /// <param name="aSearchOption">If sub directories should be copied.</param> /// <param name="aConflictOption">How to handle name conflicts.</param> public static void Copy(String aSource, String aDestination, DirectorySearchOption aSearchOption, NameConflictOption aConflictOption) { if (Directory.Exists(aSource)) { if (!Directory.Exists(aDestination)) { Directory.CreateDirectory(aDestination); } //Now Create all of the directories foreach (string dirPath in DirectoryExt.GetDirectories(aSource, aSearchOption)) { Create(dirPath.Replace(aSource.endDir(), aDestination.endDir())); } //Copy all the files foreach (string copyFrom in DirectoryExt.GetFiles(aSource, aSearchOption)) { String copyTo = copyFrom.Replace(aSource.endDir(), aDestination.endDir()); if (File.Exists(copyTo)) { if (aConflictOption == NameConflictOption.Cancel) { return; } else if (aConflictOption == NameConflictOption.Skip) { continue; } } FileExt.Copy(copyFrom, copyTo, aConflictOption); } } }
public static void Copy(String source, List <String> destinations, NameConflictOption aNameConflictOption, Action <ProcessProgress> handler, ref int cancel) { if (destinations.isEmpty()) { throw new ArgumentNullException("You must specify a destinatino location(s)"); } List <DestinationInfo> files = new List <DestinationInfo>(); foreach (String file in destinations) { if (File.Exists(file)) { switch (aNameConflictOption) { case NameConflictOption.Cancel: return; case NameConflictOption.Overwrite: files.Add(new DestinationInfo(file)); break; case NameConflictOption.RenameExisting: File.Move(file, FileExt.MakeUnique(file)); break; case NameConflictOption.RenameNew: files.Add(new DestinationInfo(FileExt.MakeUnique(file))); break; case NameConflictOption.Skip: continue; default: break; } } else { files.Add(new DestinationInfo(file)); } } if (files.Count == 0) { return; } // If we are only copying to one location don't use the multi copy else if (files.Count == 1) { Copy(source, files[0].Path, aNameConflictOption, handler, ref cancel); } else { CopyFileToMultipleDestinations(source, files, handler, ref cancel); } }
/// <summary> /// Delete all files in the given directory that match the provided pattern. /// </summary> /// <param name="aPath">The directory where the files should be found.</param> /// <param name="aSearchOption">The directory search options.</param> /// <returns>If the deletion was successful.</returns> public static bool DeleteFiles(String aPath, DirectorySearchOption aSearchOption) { bool success = true; if (Directory.Exists(aPath)) { success = FileExt.Delete(DirectoryExt.GetFiles(aPath, aSearchOption)); } return(success); }
public static void Copy(String source, String destination, NameConflictOption aNameConflictOption, Action <ProcessProgress> handler, ref int cancel) { if (!File.Exists(source)) { throw new FileNotFoundException("The source file could not be found."); } if (File.Exists(destination)) { switch (aNameConflictOption) { case NameConflictOption.Cancel: case NameConflictOption.Skip: return; case NameConflictOption.Overwrite: Delete(destination); break; case NameConflictOption.RenameExisting: FileExt.Move(destination, FileExt.MakeUnique(destination)); break; case NameConflictOption.RenameNew: destination = FileExt.MakeUnique(destination); break; default: break; } } if (!Directory.Exists(Path.GetDirectoryName(destination))) { DirectoryExt.Create(Path.GetDirectoryName(destination)); } FileInfo fileInfo = new FileInfo(source); bool noBuffer = fileInfo.Length < Constants.USE_BUFFER_FILE_SIZE_BYTES; XCopy.Copy(source, destination, aNameConflictOption == NameConflictOption.Overwrite, noBuffer, handler, ref cancel); }
private static void CopyFileToMultipleDestinations(String source, IEnumerable <DestinationInfo> destinations, Action <ProcessProgress> handler, ref int cancel) { BufferedStream sourceStream = null; ProcessProgress progress = null; Exception exception = null; byte[] sourceBuffer = new byte[Constants.COPY_BUFFER_SIZE]; try { sourceStream = new BufferedStream(File.OpenRead(source)); progress = new ProcessProgress((int)sourceStream.Length, handler); foreach (DestinationInfo file in destinations) { FileExt.Create(file.Path, NameConflictOption.Overwrite); file.BufferStream = new BufferedStream(File.OpenWrite(file.Path)); } while (sourceStream.Position < sourceStream.Length) { if (cancel == 1) { progress.Canceled = true; break; } int streamLength = sourceStream.Read(sourceBuffer, 0, Constants.COPY_BUFFER_SIZE); foreach (DestinationInfo file in destinations) { file.BufferStream.Write(sourceBuffer, 0, streamLength); } progress.Processed = sourceStream.Position; } progress.Completed = true; } catch (Exception ex) { if (progress != null) { progress.Canceled = true; } exception = ex; } finally { foreach (DestinationInfo file in destinations) { if (file.BufferStream != null) { file.BufferStream.Close(); } } if (sourceStream != null) { sourceStream.Close(); } } if (exception != null) { throw exception; } }