c# - 下次打开csv文件时如何在下一行写?

标签 c# winforms csv line

第一次运行时,我的程序在第一行写入一个 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/

相关文章:

ios - 如何从 NSURLRequest 获取可变数据

c# - c# 中的 "this"是什么?静态字段如何成为 "null"?

c# - 数组列表排序

c# - String.Format 未按预期工作 C#

c# - Excel 工作表修改当前工作表 C#

c# - Trim 方法不会清除所有空格

c# - 如何创建一个 C# 应用程序来决定是显示为控制台应用程序还是窗口应用程序?

时间:2019-03-08 标签:c#datetimepickersettime

python - Dask 读取 csv 与 pandas 读取 csv

Java - 必须在您的配置中设置主 URL