private static Menu NewMainMenu(Menu searchMenu, IContactsStorage storage) { var mainMenu = new Menu("Menu:"); mainMenu.AddItem(new MenuItem("View all contacts", () => { IO.PrintContactList("All contacts", storage.GetAllContacts()); })); mainMenu.AddItem(new MenuItem("Search", searchMenu.Invoke)); mainMenu.AddItem(new MenuItem("New contact", () => { var newContact = IO.ReadContact(); storage.AddContact(newContact, out string message); Console.WriteLine(message); })); mainMenu.AddItem(new MenuItem("Load contacts from VCard", () => { IO.LoadContactsFromVCard(IO.ReadString("Filename: "), storage); })); mainMenu.AddItem(new MenuItem("Save contacts to VCard", () => { string filename = IO.ReadString("Filename: "); if (File.Exists(filename)) { Console.WriteLine($"File \"{filename}\" already exists. Rewrite?"); if (!IO.ReadBoolean(yesByDefault: false)) { return; } } IO.SaveContactsToVCard(filename, storage); })); return(mainMenu); }
public static void SaveContactsToVCard(string filename, IContactsStorage storage) { try { File.WriteAllText(filename, Contact.ToVCardMany(storage.GetAllContacts())); Console.WriteLine("Done."); } catch (Exception e) when( e is ArgumentException || e is PathTooLongException || e is DirectoryNotFoundException || e is IOException || e is UnauthorizedAccessException || e is NotSupportedException ) { Console.WriteLine($"Error! {e.Message}"); } catch (AggregateException notFlattenedAe) { AggregateException ae = notFlattenedAe.Flatten(); Console.WriteLine($"Couldn't fetch contacts: {ae.InnerExceptions[ae.InnerExceptions.Count - 1].Message}"); } }