How to replace same text in a text file
Posted
by
user1688220
on Stack Overflow
See other posts from Stack Overflow
or by user1688220
Published on 2012-09-22T03:34:31Z
Indexed on
2012/09/22
3:37 UTC
Read the original article
Hit count: 160
i created a c# windows login form and i am saving username or password to a text file but every time i use same username or password that i have saved before it takes new place in that text file.
But What i want is to replace the same username or password that is already saved in that text file.
this is my code:
private void button1_Click(object sender, EventArgs e)
{
try
{
FileStream fs = new FileStream("data.txt", FileMode.Append,
FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write("Email ID: ");
sw.WriteLine(textBox1.Text);
sw.Write("Password: ");
sw.Write(textBox2.Text);
sw.WriteLine();
sw.WriteLine();
sw.Flush();
sw.Close();
fs.Close();
}
catch (Exception)
{
MessageBox.Show("Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
MessageBox.Show("DONE", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Clear();
textBox2.Clear();
}
© Stack Overflow or respective owner