可以写入 csv 文件但不能追加

Can write to csv file but not append

string pathDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        string filePath = pathDesktop + "\mycsvfile.csv";
        string delimter = ",";
        string a = "Enoelpro---[037,033,0,018,030,012,004,021,009,038,035,053,044,050,074,F,018,010,070,000]<Vulpix>[-][037,034,0,022,020,029,002,008,024,036,046,049,041,057,077,F,018,005,070,000]<Vulpix>[-] cual es mejor??";
        List<string[]> test = conv(a,"testrainer");
        int length = test.Count;

        using (TextWriter writer = File.CreateText(filePath))
        {
            for (int index = 0; index < length; index++)
            {
                writer.WriteLine(string.Join(delimter, test[index]));
            }
        }

所以,目前,这工作正常,只是它没有将旧数据保留在 csv 文件中。我该如何修改它而不是删除数据,它只是附加到数据?

你能用 StreamWriter class 试试吗?

If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.

改为使用 TextWriter writer = File.CreateText(filePath) 尝试使用

TextWriter writer = new StreamWriter(filePath, true);

如果您在构造函数中传递 true,它应该将文本附加到文件。

File.CreateText Method (String)

This method is equivalent to the StreamWriter(String, Boolean) constructor overload with the append parameter set to false. If the file specified by path does not exist, it is created. If the file does exist, its contents are overwritten. Additional threads are permitted to read the file while it is open.

StreamWriter Constructor (String, Boolean)

这里是第二个参数,true表示向文件追加数据; false 覆盖文件。

如果您查看每种方法的文档,它会清楚地说明您的问题的答案,并且在您需要附加文件时也会有建议。将 StreamWriter 构造函数与路径和追加参数一起使用 (true)