/// <summary> /// Deletes all subdirectory/contents in directory /// </summary> /// <param name="directoryPath"></param> /// <param name="giveWarning"></param> /// <param name="recursiveDelete"></param> public static void DeleteDirectoryContents(string directoryPath, bool giveWarning = false, bool recursiveDelete = false) { //check if the file exists as a safety measure, if not then stop if (Directory.Exists(directoryPath) == false) { //get our message string message = ""; if (string.IsNullOrEmpty(directoryPath) || string.IsNullOrWhiteSpace(directoryPath)) { message = "No given file to delete!"; } else { message = directoryPath + " - given directory does not exist! Can't delete this directory!"; } //notify the user that this action is not possible MessageBoxes.Error(message, "ERROR"); //dont continue with the rest of the function return; } if (giveWarning) { //get our message string message = "Are you sure you want to delete the contents in the following directory? " + directoryPath; //warn the user about the action if (MessageBoxes.Warning_Confirm(message, "Warning") == false) { //dont continue with the rest of the function return; } } try { //delete the directory foreach (string subDirectory in Directory.GetDirectories(directoryPath)) { Directory.Delete(subDirectory, recursiveDelete); } } catch (Exception e) { //we have an IO exception string message = string.Format("Can't delete the directory contents! Exception: {0}. Exception Message: {1}", e, e.Message); //notify the user that this action is not possible MessageBoxes.Error(message, "ERROR"); } }
/// <summary> /// Delete a given file. /// </summary> /// <param name="originalFilePath"></param> /// <param name="giveWarning"></param> public static void DeleteFile(string originalFilePath, bool giveWarning = false) { //check if the file exists as a safety measure, if not then stop if (File.Exists(originalFilePath) == false) { //get our message string message = ""; if (string.IsNullOrEmpty(originalFilePath) || string.IsNullOrWhiteSpace(originalFilePath)) { message = "No given file to delete!"; } else { message = originalFilePath + " - given file does not exist! Can't delete this file!"; } //notify the user that this action is not possible MessageBoxes.Error(message, "ERROR"); //dont continue with the rest of the function return; } if (giveWarning) { //get our message string message = "Are you sure you want to delete the following file? " + originalFilePath; //warn the user about the action if (MessageBoxes.Warning_Confirm(message, "Warning") == false) { //dont continue with the rest of the function return; } } try { //delete the file File.Delete(originalFilePath); } catch (Exception e) { //we have an IO exception string message = string.Format("Can't Delete the file! Exception: {0}. Exception Message: {1}", e, e.Message); //notify the user that this action is not possible MessageBoxes.Error(message, "ERROR"); } }
/// <summary> /// Move a given directory. /// </summary> /// <param name="oldDirectoryPath"></param> /// <param name="newDirectoryPath"></param> /// <param name="giveWarning"></param> public static void MoveDirectory(string oldDirectoryPath, string newDirectoryPath, bool giveWarning = false) { //check if the irectory exists as a safety measure, if not then stop if (Directory.Exists(oldDirectoryPath) == false) { //get our message string message = ""; if (string.IsNullOrEmpty(oldDirectoryPath) || string.IsNullOrWhiteSpace(oldDirectoryPath)) { message = "No given directory path to move!"; } else { message = oldDirectoryPath + " - given create does not exist! Can't delete this directory!"; } //notify the user that this action is not possible MessageBoxes.Error(message, "ERROR"); //dont continue with the rest of the function return; } if (giveWarning) { //get our message string message = "Are you sure you want to move the following directory? " + newDirectoryPath; //warn the user about the action if (MessageBoxes.Warning_Confirm(message, "Warning") == false) { //dont continue with the rest of the function return; } } try { //delete the file Directory.Move(oldDirectoryPath, newDirectoryPath); } catch (Exception e) { //we have an IO exception string message = string.Format("Can't move the directory! Exception: {0}. Exception Message: {1}", e, e.Message); //notify the user that this action is not possible MessageBoxes.Error(message, "ERROR"); } }
/// <summary> /// Duplicate/Copy a file, this will output the copied file path /// </summary> /// <param name="originalFilePath"></param> /// <param name="copiedFilePath"></param> /// <param name="copiedSuffix"></param> public static void DuplicateFile(string originalFilePath, out string copiedFilePath, string copiedSuffix = "_duplicated") { copiedFilePath = "null"; //check if the file exists as a safety measure, if not then stop if (File.Exists(originalFilePath) == false) { //get our message string message = ""; if (string.IsNullOrEmpty(originalFilePath) || string.IsNullOrWhiteSpace(originalFilePath)) { message = "No given file to duplicate!"; } else { message = originalFilePath + " - given file does not exist! Can't duplicate this file!"; } //notify the user that this action is not possible MessageBoxes.Error(message, "ERROR"); //dont continue return; } //build the new file path string from the original string originalFileExtension = Path.GetExtension(originalFilePath); string originalFilename = Path.GetFileNameWithoutExtension(originalFilePath); string originalPath = Path.GetDirectoryName(originalFilePath); string newFileName = originalFilename + copiedSuffix + originalFileExtension; copiedFilePath = originalPath + "/" + newFileName; try { //duplicate the file File.Copy(originalFilePath, copiedFilePath); } catch (Exception e) { //we have an exception string message = string.Format("Can't Copy the file! Exception: {0}. Exception Message: {1}", e, e.Message); //notify the user that this action is not possible MessageBoxes.Error(message, "ERROR"); } }