using System; using System.Collections.Generic; using System.Text; using System.IO; namespace PhoneBook { class FileIO { public string name; public string lastname; public string phone; //public FileIO(string _name, string _phone) //{ // name = _name; // phone = _phone; //} public void add() { StreamWriter fw = File.AppendText("phones.txt"); fw.WriteLine("Lastname: " + lastname + " " + "Name: " + name + " " + "Phone: " + phone); fw.Close(); } public string searchbyName() { string buffer = null; string RecordSearched = ""; int compareResult = 0; int counter = 0; if (!(File.Exists("phones.txt"))) { RecordSearched = "Error, no phone index"; } else { StreamReader fr = File.OpenText("phones.txt"); while ((buffer = fr.ReadLine()) != null) { string bufferNonspaced = buffer.Replace(" ", ""); if ((compareResult = (bufferNonspaced.IndexOf("Name:" + name))) != -1) { counter++; RecordSearched += counter + "# " + buffer + Environment.NewLine; } } fr.Close(); if (counter == 0) RecordSearched = "Not Found"; } return RecordSearched; } public string searchbyLastName() { string buffer = null; string RecordSearched = ""; int compareResult = 0; int counter = 0; if (!(File.Exists("phones.txt"))) { RecordSearched = "Error, no phone index"; } else { StreamReader fr = File.OpenText("phones.txt"); while ((buffer = fr.ReadLine()) != null) { string bufferNonspaced = buffer.Replace(" ", ""); if ((compareResult = (bufferNonspaced.IndexOf("Lastname:" + lastname))) != -1) { counter++; RecordSearched += counter + "# " + buffer + Environment.NewLine; } } fr.Close(); if (counter == 0) RecordSearched = "Not Found"; } return RecordSearched; } public string deletebyName() { string buffer = null; bool error = false; string RecordDeleted = null; int compareResult = 0; int counter = 0; if (!(File.Exists("phones.txt"))) { RecordDeleted = "No phone index"; error = true; } else { StreamWriter fw = new StreamWriter("temp.txt"); StreamReader fr = File.OpenText("phones.txt"); while ((buffer = fr.ReadLine()) != null) { string bufferNonspaced = buffer.Replace(" ", ""); if ((compareResult = (bufferNonspaced.IndexOf("Name:" + name))) != -1) { counter++; RecordDeleted +=counter + "# " + buffer + " Deleted" + Environment.NewLine; } else { fw.WriteLine(buffer); } } fr.Close(); fw.Close(); if (counter == 0) RecordDeleted = "Not Found"; } File.Delete("phones.txt"); if (!error) { File.Move("temp.txt", "phones.txt"); } return RecordDeleted; } public string deletebyLastName() { string buffer = null; bool error = false; string RecordDeleted = null; int compareResult = 0; int counter = 0; if (!(File.Exists("phones.txt"))) { RecordDeleted = "No phone index"; error = true; } else { StreamWriter fw = new StreamWriter("temp.txt"); StreamReader fr = File.OpenText("phones.txt"); while ((buffer = fr.ReadLine()) != null) { string bufferNonspaced = buffer.Replace(" ", ""); if ((compareResult = (bufferNonspaced.IndexOf("Lastname:" + lastname))) != -1) { counter++; RecordDeleted += counter + "# " + buffer + " Deleted" + Environment.NewLine; } else { fw.WriteLine(buffer); } } fr.Close(); fw.Close(); if (counter == 0) RecordDeleted = "Not Found"; } File.Delete("phones.txt"); if (!error) { File.Move("temp.txt", "phones.txt"); } return RecordDeleted; } } }