/// <summary> /// Copies a file and overwrite existing files if desired. /// </summary> /// <param name="sourceFilePath">Full source path</param> /// <param name="targetFilePath">Full target path</param> /// <param name="overwrite">true to overwrite existing files</param> /// <returns>True if copy succeeded, false if not. Check last Win32 Error to get further information.</returns> public static bool CopyFile(PathInfo sourceFilePath, PathInfo targetFilePath, bool overwrite = false) { bool failOnExists = !overwrite; bool result = Win32SafeNativeMethods.CopyFile(sourceFilePath.FullNameUnc, targetFilePath.FullNameUnc, failOnExists); if (!result) { int win32Error = Marshal.GetLastWin32Error(); NativeExceptionMapping(sourceFilePath.FullName, win32Error); } return(result); }
/// <summary> /// Copies a file and overwrite existing files if desired. /// </summary> /// <param name="sourceFilePath">Full source path</param> /// <param name="targetFilePath">Full target path</param> /// <param name="win32Error">Last error occured</param> /// <param name="overwrite">true to overwrite existing files</param> /// <returns>True if copy succeeded, false if not. Check last Win32 Error to get further information.</returns> /// <exception cref="PathNotFoundException">This error is fired if the specified path or a part of them does not exist.</exception> public static bool CopyFile(string sourceFilePath, string targetFilePath, out Int32 win32Error, bool overwrite = false) { Contract.Requires(!String.IsNullOrWhiteSpace(sourceFilePath)); Contract.Requires(!String.IsNullOrWhiteSpace(targetFilePath)); if (!Win32SafeNativeMethods.CopyFile(sourceFilePath, targetFilePath, !overwrite)) { win32Error = Marshal.GetLastWin32Error(); return(false); } win32Error = 0; return(true); }