【问题标题】:How can I save my textbox text in a file after previously saved text? [duplicate]以前保存文本后,如何将文本框文本保存在文件中? [复制]
【发布时间】:2018-04-18 04:14:23
【问题描述】:

当我按下按钮文本框文本保存在文件中时,我有两个文本框,但是当我再次输入新文本时,它会替换以前保存的文本。 我的代码:

using (StreamWriter strw = new StreamWriter("E:\\win part\\Discrete Mathematics\\userrequest.txt"))
{               
  strw.Write(nametextBox.Text+"\t");
  strw.WriteLine(passwordtextBox.Text);
  strw.Close();
  MessageBox.Show("Yor request has been submitted successfully.", "Sucess", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

我想在之前保存的文本之后保存这个新文本。 请帮我解决这个问题。

【问题讨论】:

  • 你可以看看StreamWriter (String, Boolean)。将 bool 设置为 true 告诉它附加到文件而不是覆盖。在您的情况下,它将是 using (StreamWriter strw = new StreamWriter("E:\\win part\\Discrete Mathematics\\userrequest.txt", true))
  • 搜索“追加”
  • File.AppendAllText("E:\\win part\\Discrete Mathematics\\userrequest.txt", nametextBox.Text + "\t" + passwordtextBox.Text); 适合你吗?

标签: c# winforms


【解决方案1】:

Adam B 正确,将 streamWrite 构造函数的 'append' 参数设置为 true:

using (StreamWriter strw = new StreamWriter("E:\\win part\\Discrete Mathematics\\userrequest.txt",true))

...

【讨论】:

  • 感谢 joe wu、Adam B、Keyur PATEL 和 Enigmativity 解决了我的问题,非常感谢。
猜你喜欢
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多