加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在下一次打开csv文件的下一行写入?

发布时间:2020-12-15 08:49:00 所属栏目:百科 来源:网络整理
导读:在第一次运行时,我的程序写入第一行的csv文件, 但是,当我在第二个……第三个时间运行我的程序时,它会在第一行运行. 我怎么能纠正它? 我想输入所有进入我程序的CSV文件. 代码如下: private void WriteToCsvFile(){ var us = users.ElementAt(0); string nam
在第一次运行时,我的程序写入第一行的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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读