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()

        private void button1_Click(object sender, EventArgs e)

        private void button2_Click(object sender, EventArgs e)
            person.Name = textBox1.Text;
            person.Car = textBox2.Text;
            person.Place = textBox3.Text;
            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?

2 Years
Discussion Span
Last Post by tinstaafl
Featured Replies
  • I think what you want is `AppendAllLines`. Something like this perhaps: File.AppendAllLines(path, person.myList); Read More


You should get line 6 out of your People class. People never is a List.
You can have a List of People.
For good design, just put in your People class what is characteristic for People.
To write to a file, just iterate (with foreach)over your People list like I showed you in my previous example. But instead of WriteString to write to the console, write to a file.Read also this.


File.AppendAllText is lookiing for a string as the second parameter. you are sending the List, not the contents of the List. as ddanbe said, you'll have to iterate over the List first, saving everything into a string and that string will be your second parameter. (use a stringbuilder.)


I think what you want is AppendAllLines. Something like this perhaps:

File.AppendAllLines(path, person.myList);  
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.