private static void Export(string parameters) { try { var options = ValidatePath(parameters); while (File.Exists(options[1])) { Console.WriteLine($"File {options[1]} already exists. Do you want to rewrite it?(y/n)"); string answer = Console.ReadLine(); if (answer.Equals("n", StringComparison.InvariantCultureIgnoreCase)) { return; } else if (!answer.Equals("y", StringComparison.InvariantCultureIgnoreCase)) { continue; } else { break; } } var snapshot = fileCabinetService.MakeSnapshot(); var writer = new StreamWriter(options[1]); if (options[0].Equals("csv", StringComparison.InvariantCultureIgnoreCase)) { snapshot.SaveToCsv(writer); } else { snapshot.SaveToXml(writer); } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private static void Export(Action <StreamWriter, FileCabinetServiceSnapshot> exportMethod) { FileCabinetServiceSnapshot snapshot = fileCabinetService.MakeSnapshot(); using (StreamWriter writer = new StreamWriter(SettingsApp.FilePath)) { exportMethod(writer, snapshot); } Console.WriteLine($"{SettingsApp.RecordsAmount} records were written to {SettingsApp.FilePath}"); }
private static void Export(string parameters) { // TODO : Process parameters var index = Array.FindIndex(exportTypes, 0, exportTypes.Length, i => string.Equals(i[Program.CommandHelpIndex], parameters.Split(' ')[0], StringComparison.InvariantCultureIgnoreCase)); if (!string.IsNullOrEmpty(parameters)) { string filePath = parameters.Split(' ')[1]; if (index == 0) { var fileInfo = new FileInfo(filePath); if (fileInfo.Exists) { Console.WriteLine($"File is exist - rewrite {filePath}, [Y/n]"); string answerRewrite = Console.ReadLine(); if (answerRewrite == "Y" || answerRewrite == "y") { Export(filePath); } else { return; } } else { Export(filePath); } } if (index == 1) { var fileInfo = new FileInfo(filePath); if (fileInfo.Exists) { Console.WriteLine($"File is exist - rewrite {filePath}, [Y/n]"); string answerRewrite = Console.ReadLine(); if (answerRewrite == "Y" || answerRewrite == "y") { ExportToXml(filePath); } else { return; } } else { ExportToXml(filePath); } } Console.WriteLine($"All records are exported to file {filePath}"); } else { Console.WriteLine("Enter type of exported file"); } void Export(string filePath) { using (var streamWriter = new StreamWriter(filePath)) { fileCabinetService.MakeSnapshot().SaveToCsv(streamWriter); } } void ExportToXml(string filePath) { using (var streamWriter = new StreamWriter(filePath)) { fileCabinetService.MakeSnapshot().SaveToXml(streamWriter); } } }
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(); } }