private void ReadFile() { // Variables to hold input. string name = "", address = "", phone = ""; int customerNumber = 0; bool mailing = false; decimal purchaseAmount = 0; try { // Read the text file. StreamReader inputFile; string inputFilePath = Environment.CurrentDirectory; string fileName = Path.Combine(inputFilePath, "contacts.txt"); inputFile = File.OpenText(fileName); // Delimiter to split string and variable to hold each line of text char[] delim = { ',' }; string line; while (!inputFile.EndOfStream) { PreferredCustomer myCustomer = new PreferredCustomer(name, address, phone, customerNumber, mailing, purchaseAmount); // Read text. line = inputFile.ReadLine(); // Split line with ','. string[] tokens = line.Split(delim); myCustomer.Name = tokens[0]; myCustomer.Phone = tokens[1]; myCustomer.Address = tokens[2]; myCustomer.CustomerNumber = int.Parse(tokens[3]); myCustomer.PurchaseAmount = decimal.Parse(tokens[4]); myCustomer.Mailing = bool.Parse(tokens[5]); customerList.Add(myCustomer); } inputFile.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void ReadFile() { // Variables to hold input. string name = "", address = "", phone = ""; int customerNumber = 0; bool mailing = false; decimal purchaseAmount = 0; try { // Read the text file. StreamReader inputFile; string inputFilePath = Environment.CurrentDirectory; string fileName = Path.Combine(inputFilePath, "contacts.txt"); inputFile = File.OpenText(fileName); // Delimiter to split string and variable to hold each line of text char[] delim = { ',' }; string line; while (!inputFile.EndOfStream) { PreferredCustomer myCustomer = new PreferredCustomer(name, address, phone, customerNumber, mailing, purchaseAmount); // Read text. line = inputFile.ReadLine(); // Split line with ','. string[] tokens = line.Split(delim); myCustomer.Name = tokens[0]; myCustomer.Phone = tokens[1]; myCustomer.Address = tokens[2]; myCustomer.CustomerNumber = int.Parse(tokens[3]); myCustomer.PurchaseAmount = decimal.Parse(tokens[4]); myCustomer.Mailing = bool.Parse(tokens[5]); customerList.Add(myCustomer); } inputFile.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }