第一次运行时,我的程序在第一行写入一个 csv 文件, 但是,当我在第二次……第三次……运行我的程序时,它运行在第一行…… 我该如何纠正它?
我想要一个 CSV 文件输入所有进入我的程序。
代码如下:
private void WriteToCsvFile()
{
var us = users.ElementAt(0);
string names = "Number',";
string userAnswer = (us.userName + ",");
foreach (string ss in user)
{
string str = Path.GetFileName(ss);
names = names + str + ",";
}
foreach (string ans in us.answer)
{
userAnswer = userAnswer + ans + ",";
}
using (StreamWriter sw = new StreamWriter("EntranceLog.csv"))
{
sw.WriteLine(names);
sw.WriteLine(userAnswer);
}
this.Close();
}
最佳答案
在构造函数中添加true
参数:
using (StreamWriter sw = new StreamWriter("EntranceLog.csv", true))
名为append
的第二个参数控制是否应覆盖或追加现有文件。 MSDN状态:
true
to append data to the file;false
to overwrite the file. If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.
关于c# - 下次打开csv文件时如何在下一行写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13107596/