可以写入csv文件,但不能追加 [英] Can write to csv file but not append

查看:53
本文介绍了可以写入csv文件,但不能追加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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文件中.我该如何修改它,而不是删除数据,而只是将其追加到数据上?

So, at the momement, this works fine, except it doesn't keep the old data in the csv file. How can I modify this so instead of deleting the data, it simply appends to the data?

推荐答案

此方法等效于 StreamWriter(String,Boolean ) append参数设置为false 的构造函数重载.如果路径指定的文件不存在,已创建.文件是否包含存在,其内容将被覆盖.允许其他线程在文件打开时读取文件.

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构造函数(字符串,布尔值)

这里是第二个参数,为true会将数据追加到文件;false覆盖文件.

Here second parameter, true to append data to the file; false to overwrite the file.

如果您查看每种方法的文档,请明确说出问题的答案,并且建议您附加文件.将StreamWriter构造函数与path和append参数一起使用(true)

If you check the documentation for each method it cleary say the answers for your questions and also there is a suggetion in case of you need to append the file. Use StreamWriter constructor with path and append parameter (true)

这篇关于可以写入csv文件,但不能追加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆