Can write to csv file but not append

前端 未结 2 1078
夕颜
夕颜 2021-01-27 06:58
string pathDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        string filePath = pathDesktop + \"\\\\mycsvfile.csv\";
        string delimter         


        
相关标签:
2条回答
  • 2021-01-27 07:32

    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)

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

    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)

    0 讨论(0)
  • 2021-01-27 07:42

    Can you please try with 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.

    Instead using TextWriter writer = File.CreateText(filePath) try to use

    TextWriter writer = new StreamWriter(filePath, true);

    If you pass true in constructor it should append text to file.

    0 讨论(0)
提交回复
热议问题