I am doing a little home project to learn more about collections and FileIO.
I have made a little racing app where the user enters the racers details all of which are strings, and these details are inserted into a List<T> list and then once the user clicks the 'save' button the contents of the list are extracted to a text file. I have successfully been able to write to the text file however I now want to change this so I am able to append the data to the text file instead of the previous data being overwritten each time the data is written to the text file, I am having trouble doing this as I am using the AppendAllText().
Here is how my People class handles the input of data to the list:
class People
{
public string Name { get; set; }
public string Car { get; set; }
public string Place { get; set; }
public List<string> myList = new List<string>();
public void ShowList()
{
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine("" + myList[i] + "\n|");
}
}
public void AddToList()
{
myList.Add("Name: " + Name + " Car: " + Car + " Place: " + Place);
}
}
and this is my Form1 class with the main method which handles the events and sends the values to the list in the People class:
public partial class Form1 : Form
{
People person = new People();
string path = @"C:\Users\Keil\Documents\Visual Studio 2013\Projects\CarRacingArrList\racers.txt";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
person.ShowList();
}
private void button2_Click(object sender, EventArgs e)
{
person.Name = textBox1.Text;
person.Car = textBox2.Text;
person.Place = textBox3.Text;
person.AddToList();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
private void button3_Click(object sender, EventArgs e)
{
File.AppendAllText(path, person.myList.OfType<string>().ToString());
}
}
As you can see I am using the AppendAllText() method where I am passing the path of the text file and the contents of the list. When data is written to the text file it appends this to the end: System.Linq.Enumerable+<OfTypeIterator>d__aa`1[System.String]
instead of the actual data stored into myList.
It looks like is returning the object type instead of the actual data
Is there something in my code that I am missing?