private static void ImportFromCsv(string path) { try { using (StreamReader reader = new StreamReader(path)) { FileCabinetServiceSnapshot snapshot = new FileCabinetServiceSnapshot(); snapshot.LoadFromCsv(reader); fileCabinetService.Restore(snapshot); } } catch (Exception ex) when( ex is ArgumentException || ex is NotSupportedException || ex is FileNotFoundException || ex is IOException || ex is System.Security.SecurityException || ex is DirectoryNotFoundException || ex is UnauthorizedAccessException || ex is PathTooLongException) { Console.WriteLine($"Import failed: can't open file {path}."); } }
private static void Import(string parameters) { try { var options = ValidatePath(parameters); if (File.Exists(options[1])) { var snapshot = fileCabinetService.MakeSnapshot(); var reader = new StreamReader(options[1]); int count; if (options[0].Equals("csv", StringComparison.InvariantCultureIgnoreCase)) { count = snapshot.LoadFromCsv(reader); } else { count = snapshot.LoadFromXml(reader); } fileCabinetService.Restore(snapshot); Console.WriteLine($"{count} records were imported."); reader.Close(); } else { throw new ArgumentException($"File with name {options[1]} doesn't exist."); } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private static void SetCommandLineSettings(string[] args) { IConfigurationRoot validationRules = null; try { validationRules = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile(Configurator.GetSetting("ValidationRulesFileName")) .Build(); } catch (FileNotFoundException) { Console.WriteLine($"{Configurator.GetConstantString("MissingValidation")} {Configurator.GetSetting("ConstantStringsFileName")}"); Console.WriteLine(Configurator.GetConstantString("ClosingProgram")); Environment.Exit(-1); } catch (FormatException) { Console.WriteLine(Configurator.GetConstantString("InvalidValidationFile")); Console.WriteLine(Configurator.GetConstantString("ClosingProgram")); Environment.Exit(-1); } Options options = GetCommandLineArguments(args); if (options.Storage.Equals(Configurator.GetConstantString("FileStorage"), StringComparison.InvariantCultureIgnoreCase)) { SetFileService(); } else if (options.Storage.Equals(Configurator.GetConstantString("MemoryStorage"), StringComparison.InvariantCultureIgnoreCase)) { SetMemoryService(); } else { Console.WriteLine($"Wrong command line argument {options.Storage}."); Environment.Exit(-1); } if (options.Rule.Equals(Configurator.GetConstantString("CustomRule"), StringComparison.InvariantCultureIgnoreCase)) { SetCustomService(validationRules); } else if (options.Rule.Equals(Configurator.GetConstantString("DefaultRule"), StringComparison.InvariantCultureIgnoreCase)) { SetDefaultService(validationRules); } else { Console.WriteLine($"Wrong command line argument {options.Rule}."); Environment.Exit(-1); } if (fileCabinetService is FileCabinetFilesystemService) { try { fileCabinetService.Restore(fileCabinetService.MakeSnapshot()); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception) #pragma warning restore CA1031 // Do not catch general exception types { Console.WriteLine(Configurator.GetConstantString("InvalidFile")); Console.WriteLine(Configurator.GetConstantString("ClosingProgram")); Environment.Exit(-1); } } if (options.Logger) { SetLogger(); } if (options.Stopwatch) { SetStopwatch(); } }