以下代码用于保存 CSV 字符串,但是,如果我保存到现有的 .CSV,而不是替换数据,它只会将新字符串添加到已经存在的数据中。我该如何补救?它是 Stream.Write 函数的工作原理所固有的,还是 Excel 和 .CSV 的特性?
SaveFileDialog dialog = new SaveFileDialog();
dialog.AddExtension = true;
dialog.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
dialog.FilterIndex = 1;
dialog.Title = "Save As";
dialog.InitialDirectory = "C:\\";
dialog.CheckPathExists = true;
dialog.DefaultExt = ".csv";
dialog.ValidateNames = true;
if (dialog.ShowDialog() == DialogResult.OK)
{
StreamWriter myStream = new StreamWriter(dialog.FileName, true);
myStream.Write(//Function which returns a CSV-formmatted string//);
myStream.Close();
OpenFile(dialog.FileName);
}