/// Taking input from the user and serialising the files to a json file and storing it in the address book public void JSonWriteData() { Console.WriteLine("Enter the first name"); string firstName = Console.ReadLine(); Console.WriteLine("Enter last name"); string lastName = Console.ReadLine(); Console.WriteLine("Enter the city"); string address = Console.ReadLine(); Console.WriteLine("Enter state"); string state = Console.ReadLine(); Console.WriteLine("Enter the zip"); string zip = Console.ReadLine(); Console.WriteLine("Enter the phone number"); string phone = Console.ReadLine(); Console.WriteLine("Enter the email"); string email = Console.ReadLine(); AddressBookMain addressBookMain = new AddressBookMain(firstName, lastName, address, state, zip, phone, email); string jsonFilePath = @"C:\Users\abhishek\Desktop\File IO\AddressBookFileStream\AddressBookFileStream\AddressBook.json"; JsonSerializer jsonSerializer = new JsonSerializer(); var writer = new StreamWriter(jsonFilePath); jsonSerializer.Serialize(writer, addressBookMain); writer.Flush(); }
public void CSVDataWriting() { Console.WriteLine("Enter the first name"); string firstName = Console.ReadLine(); Console.WriteLine("Enter last name"); string lastName = Console.ReadLine(); Console.WriteLine("Enter the city"); string address = Console.ReadLine(); Console.WriteLine("Enter state"); string state = Console.ReadLine(); Console.WriteLine("Enter the zip"); string zip = Console.ReadLine(); Console.WriteLine("Enter the phone number"); string phone = Console.ReadLine(); Console.WriteLine("Enter the email"); string email = Console.ReadLine(); AddressBookMain addressBookMain = new AddressBookMain(firstName, lastName, address, state, zip, phone, email); string csvFilePath = @"C:\Users\abhishek\Desktop\File IO\AddressBookFileStream\AddressBookFileStream\AddressBook.csv"; using (var writer = new StreamWriter(csvFilePath)) using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) { string record = firstName + "," + lastName + "," + address + "," + state + "," + zip + "," + phone + "," + email; csv.WriteHeader <AddressBookMain>(); csv.NextRecord(); csv.WriteRecord <AddressBookMain>(addressBookMain); writer.Flush(); } }
/// Adds data to the addressbook list public void AddToAddressBook(string firstName, string lastName, string address, string state, string zip, string phone, string email) { AddressBookMain addressBookMain = new AddressBookMain(firstName, lastName, address, state, zip, phone, email); addressBook.Add(addressBookMain.firstName); addressBook.Add(addressBookMain.lastName); addressBook.Add(addressBookMain.address); addressBook.Add(addressBookMain.state); addressBook.Add(addressBookMain.zip); addressBook.Add(addressBookMain.phone); addressBook.Add(addressBookMain.email); }