private static void CopyWithMethod(string sourcePath, string targetPath, FileCopyMethod fileCopyMethod, bool overWrite) { try { switch (fileCopyMethod) { case FileCopyMethod.Copy: File.Copy(sourcePath, targetPath, overWrite); break; case FileCopyMethod.Hardlink: // Delete existing file/hardlink. if (File.Exists(targetPath)) { File.Delete(targetPath); } // If the operation fails, copy the file with replacement. if (CreateHardLink(targetPath, sourcePath, IntPtr.Zero) == false) { File.Copy(sourcePath, targetPath, overWrite); } break; } } catch (IOException) // File already exists. { } catch (Exception ex) { throw new Exception($"[RelativePaths] Tried to overwrite/copy a file and failed. {targetPath}", ex); } }
/// <summary> /// Copies a file from A to B using the specified target method. Assumes target directory already exists. /// </summary> /// <param name="fileCopyMethod">The method bu which the file is meant to be copied.</param> /// <param name="sourcePath">The path where the file that is to be copied lies.</param> /// <param name="targetPath">The path where the file at sourcePath is to be copied to.</param> /// <param name="overWrite">Declares whether the files should be overwritten or not.</param> private static void CopyWithMethod(string sourcePath, string targetPath, FileCopyMethod fileCopyMethod, bool overWrite) { try { switch (fileCopyMethod) { case FileCopyMethod.Copy: File.Copy(sourcePath, targetPath, overWrite); break; case FileCopyMethod.Hardlink: // Try creating hardlink. // If the operation fails, copy the file with replacement. if (CreateHardLink(targetPath, sourcePath, IntPtr.Zero) == false) { File.Copy(sourcePath, targetPath, overWrite); } break; } } catch (IOException) // File already exists. { } catch (Exception) { MessageBox.Show($"[RelativePaths] Tried to overwrite/copy a file and failed. {targetPath}"); } }
/// <summary> /// Copies a file from A to B using the specified target method. Assumes target directory already exists. /// </summary> /// <param name="fileCopyMethod">The method bu which the file is meant to be copied.</param> /// <param name="sourcePath">The path where the file that is to be copied lies.</param> /// <param name="targetPath">The path where the file at sourcePath is to be copied to.</param> private static void CopyWithMethod(string sourcePath, string targetPath, FileCopyMethod fileCopyMethod) { switch (fileCopyMethod) { case FileCopyMethod.Copy: File.Copy(sourcePath, targetPath, true); break; case FileCopyMethod.Hardlink: // Try creating hardlink. // If the operation fails, copy the file with replacement. if (CreateHardLink(targetPath, sourcePath, IntPtr.Zero) == false) { File.Copy(sourcePath, targetPath, true); } break; } }
/// <summary> /// Copies a list of files by relative path from a list of relative paths to a specified set target directory. /// </summary> /// <param name="sourceDirectory">Specifies the source directory from which the files are meant to be copied from. Should not end on a back/forward slash.</param> /// <param name="targetDirectory">Specifies the arget directory to which the files are meant to be copied. Should not end on a back/forward slash.</param> /// <param name="fileCopyMethod">Specifies the way the files will be copied from A to B.</param> /// <param name="overWrite">Declares whether the files should be overwritten or not.</param> /// <param name="onlyNewer">Will only copy the file if the file is newer than the existing file.</param> public static void CopyByRelativePath(string sourceDirectory, string targetDirectory, FileCopyMethod fileCopyMethod, bool overWrite, bool onlyNewer) { // Obtain the relative paths to the target directory. List <string> relativePaths = GetRelativeFilePaths(sourceDirectory); // Call the other overload. CopyByRelativePath(relativePaths, sourceDirectory, targetDirectory, fileCopyMethod, overWrite, onlyNewer); }
/// <summary> /// Copies a list of files by relative path from a list of relative paths to a specified set target directory. /// </summary> /// <param name="relativePaths">Specifies the relative file paths which are to be copied.</param> /// <param name="sourceDirectory">Specifies the source directory from which the files are meant to be copied from. Should not end on a back/forward slash.</param> /// <param name="targetDirectory">Specifies the arget directory to which the files are meant to be copied. Should not end on a back/forward slash.</param> /// <param name="fileCopyMethod">Specifies the way the files will be copied from A to B.</param> /// <param name="overWrite">Declares whether the files should be overwritten or not.</param> public static void CopyByRelativePath(List <string> relativePaths, string sourceDirectory, string targetDirectory, FileCopyMethod fileCopyMethod, bool overWrite) { CopyByRelativePath(relativePaths, sourceDirectory, targetDirectory, fileCopyMethod, overWrite, false); }
/// <summary> /// Copies a list of files by relative path from a list of relative paths to a specified set target directory. /// </summary> /// <param name="relativePaths">Specifies the relative file paths which are to be copied.</param> /// <param name="sourceDirectory">Specifies the source directory from which the files are meant to be copied from. Should not end on a back/forward slash.</param> /// <param name="targetDirectory">Specifies the arget directory to which the files are meant to be copied. Should not end on a back/forward slash.</param> /// <param name="fileCopyMethod">Specifies the way the files will be copied from A to B.</param> /// <param name="overWrite">Declares whether the files should be overwritten or not.</param> /// <param name="onlyNewer">Will only copy the file if the file is newer than the existing file.</param> private static void CopyByRelativePath(List <string> relativePaths, string sourceDirectory, string targetDirectory, FileCopyMethod fileCopyMethod, bool overWrite, bool onlyNewer) { // For each relative path. foreach (string relativePath in relativePaths) { // Get the target path. string targetPath = targetDirectory + relativePath; // Get the source path. string sourcePath = sourceDirectory + relativePath; // Confirm source, and target's directory exist. if (!File.Exists(sourcePath)) { continue; } // Check if the file is newer or not. if (onlyNewer && File.Exists(targetPath)) { if (File.GetLastWriteTime(sourcePath).ToUniversalTime() < File.GetLastWriteTime(targetPath).ToUniversalTime()) { continue; // Source file is older, proceed to next. } } if (!Directory.Exists(Path.GetDirectoryName(targetPath))) { Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); } // Copy the files from A to B using the specified target method. CopyWithMethod(sourcePath, targetPath, fileCopyMethod, overWrite); } }
/// <summary> /// Copies a list of files by relative path from a list of relative paths to a specified set target directory. /// </summary> /// <param name="relativePaths">Specifies the relative file paths which are to be copied.</param> /// <param name="sourceDirectory">Specifies the source directory from which the files are meant to be copied from. Should not end on a back/forward slash.</param> /// <param name="targetDirectory">Specifies the arget directory to which the files are meant to be copied. Should not end on a back/forward slash.</param> /// <param name="fileCopyMethod">Specifies the way the files will be copied from A to B.</param> public static void CopyByRelativePath(List <string> relativePaths, string sourceDirectory, string targetDirectory, FileCopyMethod fileCopyMethod) { // For each relative path. foreach (string relativePath in relativePaths) { // Get the target path. string targetPath = targetDirectory + relativePath; // Get the source path. string sourcePath = sourceDirectory + relativePath; // Confirm source, and target's directory exist. if (!File.Exists(sourcePath)) { continue; } if (!Directory.Exists(Path.GetDirectoryName(targetPath))) { Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); } // Copy the files from A to B using the specified target method. CopyWithMethod(sourcePath, targetPath, fileCopyMethod); } }
/// <summary> /// Copies files from the <see cref="sourceDirectory"/> to the <see cref="targetDirectory"/> provided a list of relative paths /// <see cref="RelativePaths"/> of files to copy. /// </summary> /// <param name="relativePaths">Relative paths of files to be copied.</param> /// <param name="sourceDirectory">Source directory to copy files from. Should not end on a back/forward slash.</param> /// <param name="targetDirectory">Target directory to copy files to. Should not end on a back/forward slash.</param> /// <param name="fileCopyMethod">Specifies the way the files will be copied from A to B.</param> /// <param name="overWrite">Declares whether the files should be overwritten or not.</param> public static void CopyByRelativePath(IEnumerable <string> relativePaths, string sourceDirectory, string targetDirectory, FileCopyMethod fileCopyMethod, bool overWrite, bool onlyNewer = false) { CopyByRelativePath(relativePaths, sourceDirectory, targetDirectory, fileCopyMethod, overWrite, onlyNewer, null); }
private static void CopyByRelativePath(IEnumerable <string> relativePaths, string sourceDirectory, string targetDirectory, FileCopyMethod fileCopyMethod, bool overWrite, bool onlyNewer, object _) { // For each relative path. foreach (string relativePath in relativePaths) { // Get copy paths. string targetPath = targetDirectory + relativePath; string sourcePath = sourceDirectory + relativePath; // Perform safety checks. if (!File.Exists(sourcePath)) { continue; } if (onlyNewer && File.Exists(targetPath)) { if (File.GetLastWriteTime(sourcePath).ToUniversalTime() < File.GetLastWriteTime(targetPath).ToUniversalTime()) { continue; } } string targetDirectoryPath = Path.GetDirectoryName(targetPath); if (!Directory.Exists(targetDirectoryPath)) { Directory.CreateDirectory(targetDirectoryPath ?? throw new InvalidOperationException("Target directory is null.")); } // Copy the files from A to B using the specified target method. CopyWithMethod(sourcePath, targetPath, fileCopyMethod, overWrite); } }