/// <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> /// Delete the sub directories in a given directory. /// </summary> /// <param name="mainDir"></param> /// <param name="warning"></param> /// <param name="promptDescription"></param> /// <param name="promptTitle"></param> public static void DeleteDirectoriesInDirectory(string mainDir, bool warning = false, string promptDescription = "", string promptTitle = "") { if (warning) { if (MessageBoxes.Warning_Confirm(promptDescription, promptTitle) == false) { return; } } foreach (string directory in Directory.GetDirectories(mainDir)) { DeleteDirectory(directory); } }
/// <summary> /// Delete the files in a given directory. /// </summary> /// <param name="directory"></param> /// <param name="warning"></param> /// <param name="promptDescription"></param> /// <param name="promptTitle"></param> public static void DeleteFilesInDirectory(string directory, bool warning = false, string promptDescription = "", string promptTitle = "") { if (warning) { if (MessageBoxes.Warning_Confirm(promptDescription, promptTitle) == false) { return; } } foreach (string filePath in Directory.GetFiles(directory)) { DeleteFile(filePath); } }