Writing data into CSV file in C#

后端 未结 15 1355
清酒与你
清酒与你 2020-11-22 17:15

I am trying to write into a csv file row by row using C# language. Here is my function

string first = reader[0].ToString();
string second=image.         


        
15条回答
  •  长情又很酷
    2020-11-22 17:56

    Simply use AppendAllText instead:

    File.AppendAllText(filePath, csv);
    

    The only downside of the AppendAllText is that it will throw error when file does not exist, so this must be checked

    Sorry, blonde moment before reading the documentation. Anyway, the WriteAllText method overwrites anything that was previously written in the file, if the file exists.

    Note that your current code is not using proper new lines, for example in Notepad you'll see it all as one long line. Change the code to this to have proper new lines:

    string csv = string.Format("{0},{1}{2}", first, image, Environment.NewLine);
    

提交回复
热议问题