/// <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); } }
/// <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> /// 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"); } }
/// <summary> /// Create a given directory /// </summary> /// <param name="newDirectoryPath"></param> /// <param name="giveWarning"></param> public static void CreateDirectory(string newDirectoryPath, bool giveWarning = false) { //check if the directory exists as a safety measure, if not then stop if (string.IsNullOrEmpty(newDirectoryPath) || string.IsNullOrWhiteSpace(newDirectoryPath)) { //get our message string message = "No given directory path to create!"; //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 create 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.CreateDirectory(newDirectoryPath); } catch (Exception e) { //we have an IO exception string message = string.Format("Can't create the directory! Exception: {0}. Exception Message: {1}", e, e.Message); //notify the user that this action is not possible MessageBoxes.Error(message, "ERROR"); } }