Reputation: 25
So I have this application where there are 3 textboxes which ask the user for input. When the user has finished filling out the texboxes, there is a button that stores the input into a class.
Now the idea is that as the user presses the button to store the input a class, the input also gets stored into a textfile. The problem that I am facing now is that whenever a user adds new input, the old input gets replaced in the textfile.
This is the code:
using (StreamWriter txtFile = new StreamWriter(@"C:\Users\Desktop\smtp.txt"))
{
txtFile.WriteLine(Class_Smtp[Index].SmtpClientName);
txtFile.WriteLine(Class_Smtp[Index].SmtpPort);
txtFile.WriteLine(Class_Smtp[Index].SmtpEmailExtension + Environment.NewLine);
}
Any Ideas?
Thank you in advance.
Upvotes: 0
Views: 259
Reputation: 103
You should append the text
using (StreamWriter txtFile = new StreamWriter(@"C:\Users\Desktop\smtp.txt", true))
{
txtFile.WriteLine(Class_Smtp[Index].SmtpClientName);
txtFile.WriteLine(Class_Smtp[Index].SmtpPort);
txtFile.WriteLine(Class_Smtp[Index].SmtpEmailExtension + Environment.NewLine);
}
Upvotes: 1
Reputation: 109567
Use the StreamWriter
constructor override that accepts a bool parameter to tell it whether to append or replace, and pass true
:
public StreamWriter(string path, bool append)
For your example:
using
(
StreamWriter txtFile = new StreamWriter
(
@"C:\Users\Desktop\smtp.txt",
true // <=== This parameter tells it to append
)
)
Upvotes: 1