public static void CompareContents(string userOutputPath, string expectedOutputPath) { string userOutputFile = IOManager.ExtractFileName(userOutputPath); string expectedOutputFile = IOManager.ExtractFileName(expectedOutputPath); userOutputPath = IOManager.BuildAbsolutePath(userOutputPath); expectedOutputPath = IOManager.BuildAbsolutePath(expectedOutputPath); Console.WriteLine("Reading files..."); try { string[] actualOutputLines = File.ReadAllLines($"{userOutputPath}\\{userOutputFile}"); string[] expectedOutputLines = File.ReadAllLines($"{expectedOutputPath}\\{expectedOutputFile}"); Console.WriteLine("Files read!"); bool hasMismatch; string[] mismatches = GetLinesWithPossibleMismatches( actualOutputLines, expectedOutputLines, out hasMismatch); Console.WriteLine("Comparison results:"); Console.WriteLine($"░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); string mismatchPath = $"{expectedOutputPath}\\mismatches.txt"; PrintOutput(mismatches, hasMismatch, mismatchPath); Console.WriteLine($"░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); } catch (Exception exception) { if (exception is DirectoryNotFoundException || exception is FileNotFoundException) { IOManager.DisplayAlert(Exceptions.InvalidPath); } } }
public static void CreateDirectory(string path) { path = IOManager.BuildAbsolutePath(path); string existingPath = String.Empty; string[] pathToCreate = path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); try { for (int l = 0; l < pathToCreate.Length; l++) { string currentPath = existingPath + pathToCreate[l]; if (!Directory.Exists(currentPath)) { Directory.CreateDirectory(currentPath); Console.WriteLine($"Directory \"{pathToCreate[l]}\" created in {existingPath}"); } existingPath = $"{currentPath}\\"; } } catch (Exception exception) { if (exception is ArgumentException || exception is NotSupportedException) { IOManager.DisplayAlert(Exceptions.InvalidName); } else if (exception is UnauthorizedAccessException) { IOManager.DisplayAlert(Exceptions.UnauthorizedAccess); } } }
public static void ChangeDirectory(string destinationPath) { string currentDir = Directory.GetCurrentDirectory(); destinationPath = IOManager.BuildAbsolutePath(destinationPath); try { Directory.SetCurrentDirectory(destinationPath); } catch (Exception exception) { if (exception is DirectoryNotFoundException) { IOManager.DisplayAlert(Exceptions.InvalidPath); } else if (exception is ArgumentOutOfRangeException) { IOManager.DisplayAlert(Exceptions.InvalidCommandParameter); } else if (exception is UnauthorizedAccessException) { IOManager.DisplayAlert(Exceptions.UnauthorizedAccess); } } }
public static void DownloadFile(string sourcePath) { string destinationPath = Directory.GetCurrentDirectory(); string fileName = IOManager.ExtractFileName(sourcePath); sourcePath = IOManager.BuildAbsolutePath(sourcePath); if (String.IsNullOrEmpty(fileName)) { IOManager.DisplayAlert(Exceptions.FileNotSpecified); } else if (!sourcePath.Contains("\\") || sourcePath == destinationPath) { IOManager.DisplayAlert(Exceptions.IncompletePath); } else { try { File.Copy($"{sourcePath}\\{fileName}", $"{destinationPath}\\{fileName}"); Console.WriteLine($"File \"{fileName}\" has been downloaded to your current working folder"); } catch (Exception exception) { if (exception is DirectoryNotFoundException || exception is FileNotFoundException) { IOManager.DisplayAlert(Exceptions.InvalidPath); } else if (exception is UnauthorizedAccessException) { IOManager.DisplayAlert(Exceptions.UnauthorizedAccess); } else if (exception is IOException) { IOManager.DisplayAlert(Exceptions.FileAlreadyDownloaded); ConsoleKeyInfo choice = Console.ReadKey(); Console.Write(Environment.NewLine); if (choice.Key == ConsoleKey.Y) { File.Copy($"{sourcePath}\\{fileName}", $"{destinationPath}\\{fileName}", true); Console.WriteLine($"File \"{fileName}\" has been overwritten."); } else { Console.WriteLine("Download cancelled."); } } } } }
public static void LoadData(string path) { string fileName = IOManager.ExtractFileName(path); path = IOManager.BuildAbsolutePath(path); try { string[] databaseSource = File.ReadAllLines($"{path}\\{fileName}"); Console.WriteLine("Populating database structure..."); string pattern = @"([A-Z]\#?\+{0,2}[a-zA-Z]*_[A-Z][a-z]{2}_201[4-8])\s+([A-Z][a-z]{0,3}\d{2}_\d{2,4})\s+(100|[1-9][0-9]|[0-9])"; database.Clear(); foreach (string record in databaseSource) { if (!string.IsNullOrEmpty(record) && Regex.IsMatch(record, pattern)) { Match validRecord = Regex.Match(record, pattern); string course = validRecord.Groups[1].Value; string student = validRecord.Groups[2].Value; int score = int.Parse(validRecord.Groups[3].Value); if (!database.ContainsKey(course)) { database.Add(course, new Dictionary <string, List <int> >()); } if (!database[course].ContainsKey(student)) { database[course].Add(student, new List <int>()); } database[course][student].Add(score); } } isDatabaseInitialized = true; Console.WriteLine($"Done! {databaseSource.Length} unique records were loaded in the database."); } catch (Exception exception) { if (exception is DirectoryNotFoundException || exception is FileNotFoundException) { IOManager.DisplayAlert(Exceptions.InvalidPath); } else if (exception is UnauthorizedAccessException) { IOManager.DisplayAlert(Exceptions.UnauthorizedAccess); } } }
public static void OpenFile(string path) { string fileName = IOManager.ExtractFileName(path); path = IOManager.BuildAbsolutePath(path); string filePath = $"{path}\\{fileName}"; try { Console.WriteLine(File.ReadAllText(filePath)); } catch (Exception exception) { if (exception is DirectoryNotFoundException || exception is FileNotFoundException) { IOManager.DisplayAlert(Exceptions.InvalidPath); } else if (exception is UnauthorizedAccessException) { IOManager.DisplayAlert(Exceptions.UnauthorizedAccess); } } }