Little problem with lists c#

Hello, im trying to create a txt file with all the catched data in the variable x. i have a boolean variable (SaveData) which will enable to start the saving of this data. but at the end in txt file i only can catch one value ( the last one before disable the boolean variable), any idea what i can do to catch all the data ?

the X variable is data from sensor

void Txt_Data()
    {
        StreamWriter File = new StreamWriter ("TestTest1.txt");
        SortedList<float,int> output = new SortedList<float,int> (){};
        if (SaveData == true) {

            j++;
            output.Add (x, j);
       

       
            foreach (KeyValuePair<float,int> listItem in output) {
                File.Write (listItem.ToString () + "\r\n");
                File.Close();

            }
        }
   

    }

https://msdn.microsoft.com/nl-be/library/8bh11f1k.aspx
seems like WriteLine will do what you need.

it still no working

What is the current result? Do you even get a file? Might be folder permissions. Do you get an empty file? Does x have a value? Do you get any errors?

WHAT is not working?

i can create a file, but still catching only one data,

Would that data be the last object in the list by any chance?

yeah, it’s the last data from the list

I hoped that last question would’ve helped you figure it out, but you are missing a parameter in the streamwriter constructor. Check here: https://msdn.microsoft.com/en-us/library/36b035cb(v=vs.110).aspx