-1

我对以下代码有疑问。它应该防止覆盖文本文件中的记录。我的代码是这样的:

    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();

}

它会覆盖记录。我想添加记录而不是覆盖它。

4

1 回答 1

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);
}
于 2013-04-26T00:26:00.767 回答