/// <summary> /// Prompts the user to input a file path followed by a file name until valid entries are made. The validated full path is returned. /// </summary> /// <param name="fileTypeDescription">A description of the file type being sought (e.g. '<c>config</c>'). For use in user prompts.</param> /// <returns>The validated full path.</returns> public static string GetUserInputFilePath(string fileTypeDescription) { string filePath = string.Empty; string fileFullPath = string.Empty; bool filePathIsValid = false; bool fileNameIsValid = false; // Get the user to enter a valid directory path. while (!filePathIsValid) { Console.ForegroundColor = Common.ConsoleColorForUserPrompts; Console.Write($"> Enter {fileTypeDescription} folder (e.g. 'C:\\Temp'):"); Console.ForegroundColor = Common.ConsoleColorForUserInput; filePath = Console.ReadLine(); Console.ForegroundColor = Common.ConsoleColorDefault; if (Directory.Exists(filePath)) { filePathIsValid = true; } else { ConsoleUtility.LogError($"The folder entered does not exist."); } } // Get the use to enter a valid filename. while (!fileNameIsValid) { Console.ForegroundColor = Common.ConsoleColorForUserPrompts; Console.Write($"> Enter {fileTypeDescription} file name (e.g. 'FileName.csv'):"); Console.ForegroundColor = Common.ConsoleColorForUserInput; string fileName = Console.ReadLine(); Console.ForegroundColor = Common.ConsoleColorDefault; fileFullPath = Path.Combine(filePath, fileName); if (File.Exists(fileFullPath)) { fileNameIsValid = true; } else { ConsoleUtility.LogError($"The file '{fileName}' does not exist in folder '{filePath}'."); } } return(fileFullPath); }
/// <summary> /// Prompts the user to input a directory path until a valid entry is made. The validated directory path is returned. /// </summary> /// <returns>The validated directory path.</returns> public static string GetUserInputDirectory() { string filePath = string.Empty; bool filePathIsValid = false; // Get the user to enter a valid directory path. while (!filePathIsValid) { Console.ForegroundColor = Common.ConsoleColorForUserPrompts; Console.Write($"> Enter a folder path (e.g. 'C:\\Temp'):"); Console.ForegroundColor = Common.ConsoleColorForUserInput; filePath = Console.ReadLine(); Console.ForegroundColor = Common.ConsoleColorDefault; if (Directory.Exists(filePath)) { filePathIsValid = true; } else { ConsoleUtility.LogError($"The folder entered does not exist."); } } return(filePath); }