C# 如何新建txt文档保存数据 或者追加数据 而不是覆盖之前的txt文档
private void Save()
{
int num = (int)Convert.ToInt16(comboBox1.SelectedItem);
if (listBox2.Items.Count == num)
{
string path = tbx_Save.Text;
string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH");
File.WriteAllText(path + "\\" + NowTime + ".txt", null);
StreamWriter sw = new StreamWriter(path + "\\" + NowTime + ".txt");
for (int i = 0; i < num && listBox2.Items.Count > 0; i++)
{
string data = listBox2.Items[0].ToString();
sw.Write(data);
sw.WriteLine();
listBox2.Items.RemoveAt(0);
}
//清空缓冲区、关闭流
sw.Flush();
sw.Close();
}
}
这是我的代码 现在问题是 当我有收到数据后 会把ListBox2中的数据保存在我指定的路径下 不断地收数据 就会覆盖前一次的txt文档 包括数据 我想保留所有数据 或者不断地新建txt吧 不想覆盖 怎么解决