我已经编写了以下代码来将数据附加到现有数据中,但是我的代码会覆盖它
我应该如何更改代码附加数据。
protected void Page_Load(object sender, EventArgs e)
{
fname = Request.Form["Text1"];
lname = Request.Form["Text2"];
ph = Request.Form["Text3"];
Empcode = Request.Form["Text4"];
string filePath = @"E:Employee.txt";
if (File.Exists(filePath))
{
//StreamWriter SW;
//SW = File.CreateText(filePath);
//SW.Write(text);
//SW.Close();
FileStream aFile = new FileStream(filePath, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(aFile);
sw.WriteLine(Empcode);
sw.WriteLine(fname);
sw.WriteLine(lname);
sw.WriteLine(ph);
sw.WriteLine("**********************************************************************");
sw.Close();
aFile.Close();
}
else
{
//sw.Write(text);
//sw.Flush();
//sw.Close();
//StreamWriter SW;
//SW = File.AppendText(filePath);
//SW.WriteLine(text);
//SW.Close();
FileStream aFile = new FileStream(filePath, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(aFile);
sw.WriteLine(Empcode);
sw.WriteLine(fname);
sw.WriteLine(lname);
sw.WriteLine(ph);
sw.WriteLine("**********************************************************************");
sw.Close();
aFile.Close();
//System.IO.File.WriteAllText(filePath, text);
}
Response.Write("Employee Add Successfully.........");
}