'C# streamwriter does not overwrite futher information [duplicate]
i'm working on a calculator project for my school.
i have a problem with overwriting my operations to a text document, normally i should have this on my text file:
1|peter|DESKTOP-P6IL7VV|19/11/2017 11:38:11 ==> 5 x 5=25
2|peter|DESKTOP-P6IL7VV|19/11/2017 11:38:15 ==> 25 + 3=28
3|peter|DESKTOP-P6IL7VV|19/11/2017 11:38:27 ==> 28 / 2=14
4|peter|DESKTOP-P6IL7VV|19/11/2017 11:38:31 ==> 14 + 6=20
5|peter|DESKTOP-P6IL7VV|19/11/2017 11:38:35 ==> 20 - 20=0
(example from my teacher)
but every time i write something, it deletes the first line, and overwrites only the last operation, i need every time i click on the result button, it overwrites to my file with the order of numbers
here is my coding:
private void result_Click(object sender, RoutedEventArgs e)
{
try
{
result();
}
catch (Exception exc)
{
txtIngave.Text = "Error!";
}
try
{
if (!txtResultaat.Text.Equals(string.Empty) && btnLog.IsChecked
== true)
{
int i = 1;
using (StreamWriter myOper = new
StreamWriter(@"C:\Users\Ach\Documents\Visual Studio
2017\Projects\TaakCalculator\log\log.txt"))
{
myOper.WriteLine(i.ToString() + " | " + "Achraf | " + System.Environment.MachineName + " | " + DateTime.Now.ToLongTimeString() + " | ==> " + txtResultaat.Text);
}
i++;
}
}
catch (Exception ex)
{
MessageBox.Show("Loser");
}
}
Solution 1:[1]
Try the
StreamWriter(string path, bool append)
constructor with the parameter append
being true
.
From the documentation:
append
true to append data to the file; false to overwrite the file. If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Paul Kertscher |