/// <summary> /// Creates a directory under the application directory /// </summary> /// <param name="directoryName">The name of the application</param> /// <param name="path">The path to the original file</param> /// <param name="confirmDestinationDelete">Whether or not to ask the user prior to the file being deleted</param> /// <param name="messageHandler">A delegate to invoke if a message needs to be displayed</param> /// <param name="messageRespondHandler">A delegate to invoke if a message needs to be displayed and responded to</param> /// <param name="destinationPath">The path to the destination file created</param> /// <returns>Whether or not the destination file was created</returns> public static bool CreateAppDataTemporaryFile(string directoryName, string path, bool confirmDestinationDelete, Message messageHandler, RespondToMessage messageRespondHandler, out string destinationPath) { string fileName = Path.GetFileName(path); destinationPath = Path.Combine(directoryName, fileName); return(CopyFile(path, destinationPath, confirmDestinationDelete, messageHandler, messageRespondHandler)); }
/// <summary> /// Copies a file from one place to another /// </summary> /// <param name="sourcePath">The source path</param> /// <param name="destinationPath">The destination path</param> /// <param name="confirmDestinationDelete">Whether to confirm deleting an existing file or not</param> /// <param name="messageHandler">The delegate to use to display messages</param> /// <param name="messageRespondHandler">The delegate to use to display message and get a response</param> /// <returns>Whether or not the file was copied or not</returns> public static bool CopyFile(string sourcePath, string destinationPath, bool confirmDestinationDelete, Message messageHandler, RespondToMessage messageRespondHandler) { if (!File.Exists(sourcePath)) { messageHandler("The file" + sourcePath + " does not exist."); return(false); } if (File.Exists(destinationPath)) { if (confirmDestinationDelete) { if (messageRespondHandler == null) { messageHandler("The file " + destinationPath + " already exists."); return(false); } MessageBoxResult result = messageRespondHandler("The file " + destinationPath + " already exists. Delete it prior to the move?"); if (!MessageBoxResponseIsAffirmitive(result)) { return(false); } } try { File.Delete(destinationPath); } catch (Exception exception) { if (messageRespondHandler != null) { messageHandler("Failed to delete the file: " + destinationPath + ". " + exception.Message); } return(false); } } try { File.Copy(sourcePath, destinationPath); } catch (Exception exception) { if (messageHandler != null) { messageHandler("Failed to copy the file: " + sourcePath + " to " + destinationPath + ". " + exception.Message); } return(false); } return(true); }