为什么我的列表<;字符串>;是';t totally writted in my.CSV
本文关键字:writted totally in CSV my gt 列表 我的 lt 字符串 为什么 | 更新日期: 2023-09-27 18:21:32
我正试图从list<string>
中用C#编写一个.csv文件,大约有350行(13列)。我在文件中写了一个循环,但文件中只写了我列表的一部分(206行半)。这是我的代码:
StreamWriter file = new StreamWriter(@"C:'test.csv", true);
foreach (string s in MyListString)
{
Console.WriteLine(s); // Display all the data
file.WriteLine(s); // Write only a part of it
}
为什么我的文件没有正确填写?有什么限制需要考虑吗?
看起来您可能需要Flush
或Close
编写器。此外,大多数时候您可能希望将编写器封装在using
语句中。
幸运的是,在处理时,它会自动关闭写入程序,刷新要写入的最后一批项,因此它还解决了您的问题,并处理了您现在处理完的任何未托管项。
尝试以下操作:
using (StreamWriter file = new StreamWriter(@"C:'test.csv", true))
{
foreach (string s in MyListString)
{
Console.WriteLine(s); // Display all the data
file.WriteLine(s); // Write only a part of it
}
}
您必须关闭流:
using(StreamWriter file = new StreamWriter(@"C:'test.csv", true))
{
foreach (string s in MyListString)
{
Console.WriteLine(s); // Display all the data
file.WriteLine(s); // Write only a part of it
}
}
using (StreamWriter file = new StreamWriter(@"C:'test.csv", true)){
foreach (string s in MyListString)
{
Console.WriteLine(s); // Display all the data
file.WriteLine(s); // Write only a part of it
}
}