我遇到以下代码的问题。它应该防止覆盖文本文件中的记录。我的代码是这样的:
public static void WritingMethod()
{
StreamWriter Sw = new StreamWriter("fileone.txt", true);
string output = string.Format("Thank you for registration! Your Submitted information are:" + Environment.NewLine + "Name: {0}"
+ Environment.NewLine + "ID: {1}" + Environment.NewLine + "Age: {2}" + Environment.NewLine + "E-mail: {3}", Name, ID, Age, Email);
// I using the Environment.NewLine to insert new lines
Console.WriteLine(output);
Sw.WriteLine(output + Environment.NewLine);
Sw.Close();
}
它会覆盖记录。我想添加记录而不是覆盖它。
答案 0 :(得分:4)
使用StreamWriter
:
File.AppendText
using (StreamWriter Sw = File.AppendText("fileone.txt"))
{
string output = string.Format("Thank you for registration! Your Submitted information are:" + Environment.NewLine + "Name: {0}"
+ Environment.NewLine + "ID: {1}" + Environment.NewLine + "Age: {2}" + Environment.NewLine + "E-mail: {3}", Name, ID, Age, Email);
Console.WriteLine(output);
Sw.WriteLine(output + Environment.NewLine);
}