Reputation: 23
I am trying to save a number of different forms to a text file, currently I have:
The issue that I'm having is that it only saves the "Cloud" data. Is there a way I can save all the information on submission and have them separated by a comma, dash, or just a space?
This is the code I'm currently using:
private void button2_Click(object sender, RoutedEventArgs e)
{
File.WriteAllText("C:\\WeatherSpotter.txt", tempC.Text);
File.WriteAllText("C:\\WeatherSpotter.txt", tempF.Text);
File.WriteAllText("C:\\WeatherSpotter.txt", windMPH.Text);
File.WriteAllText("C:\\WeatherSpotter.txt", windKM_Textbox.Text);
File.WriteAllText("C:\\WeatherSpotter.txt", Cloud_ComboBox.Text);
}
Upvotes: 1
Views: 3121
Reputation: 48086
Each time you invoke File.WriteAllText
you overwrite the existing file. You instead should be using an append function such as File.AppendText()
or opening the file, writing each of the lines, then closing it. You could also use StringBuilder
or something to build the entire file contents.
There are so many options, just be sure to check how the method you choose handles the file (what mode/permissions it uses).
Upvotes: 1
Reputation: 887453
File.WriteAllText
replaces the contents of the file.
You want File.AppendText()
, which does exactly what it says on the tin.
Note that you may or may not want the first call to still be WriteAllText()
.
Or, better yet, concatenate all of that into a single string and call WriteAllText()
only once.
Upvotes: 4