public static string GetDirectoryPath() { string path = ""; char[] badChars = { '/', '\\', ':', '*', '?', '<', '>', '"', '|' }; Write("Enter full directory path : "); do { path = ReadLine(); if (GoodPath(path)) { WriteLine("Your path is good ! \n"); break; } else { UserInteraction.InvalidInputMessage(); } } while (true); return(path); }
public static string GetFilePath() { string path = ""; string fileName = ""; char[] badChars = { '/', '\\', ':', '*', '?', '<', '>', '"', '|' }; Write("Enter file name : "); do { fileName = ReadLine(); Write("Enter path to this file (without name) : "); path = ReadLine(); if (GoodPath(path) && GoodFileName(fileName)) { WriteLine("Your path ande name are good ! \n"); break; } else { UserInteraction.InvalidInputMessage(); } } while (true); return(path); }
public static string GetFileOrFolderName() { string name; while (true) { name = ReadLine(); if (GoodFileOrFolderName(name)) { break; } else { UserInteraction.InvalidInputMessage(); } } return(name); }
private static bool GoodPath(string path) { bool niceInput = true; char[] badNameChars = { '/', ':', '*', '?', '<', '>', '"', '|' }; char[] badDiskChars = { '/', '\\', '*', '?', '<', '>', '"', '|' }; // Stores path in convinient format. // [0] - drive name, [1] - ":", [2] - "\", [3] - name1, [4] - "\", [5] - name2 .. List <string> separatedPath = new List <string>(); for (int i = 0; i < path.Length; i++) { separatedPath.Add(""); } for (int i = 0, j = 0; i < path.Length; i++) { if (j == 0) { if (path[i] == ':') { if (separatedPath[j].Length == 0 || !DriveExists(separatedPath[j])) { niceInput = false; break; } else { j++; separatedPath[j] = ":"; j++; } } else { // Check if path[i] char is correct. foreach (var ourChar in badDiskChars) { if (ourChar == path[i]) { niceInput = false; break; } } separatedPath[j] += path[i]; } } else if (j % 2 == 0) { if (path[i] == '\\') { separatedPath[j] = "\\"; j++; } else { niceInput = false; break; } } else // Here we checks for correct name. { if (path[i] == '\\') { if (separatedPath[j].Length == 0) { niceInput = false; break; } else { j++; separatedPath[j] = path[i].ToString(); j++; } } else { foreach (var ourChar in badNameChars) { if (path[i] == ourChar) { UserInteraction.InvalidInputMessage(); break; } } separatedPath[j] += path[i]; } } } // End for. return(niceInput); }