C# 为什么我的名单<;字符串>;isn';在我的.CSV中没有完全写入?
我正试图从一个大约有350行(13列)的C# 为什么我的名单<;字符串>;isn';在我的.CSV中没有完全写入?,c#,csv,streamwriter,C#,Csv,Streamwriter,我正试图从一个大约有350行(13列)的列表中用C#编写一个.csv文件。 我使用循环写入文件,但只有列表的一部分写入文件(206行半)。 这是我的代码: StreamWriter file = new StreamWriter(@"C:\test.csv", true); foreach (string s in MyListString) { Console.WriteLine(s); // Display all th
列表中用C#编写一个.csv文件。
我使用循环写入文件,但只有列表的一部分写入文件(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
}
为什么我的文件填写不正确?是否需要考虑任何限制?您必须关闭您的流:
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
}
}
看起来您可能需要刷新写入程序或关闭写入程序。此外,大多数情况下,您可能希望使用
语句将编写器包装在一个中
幸运的是,在dispose上,它会自动关闭写入程序,刷新要写入的最后一批项,因此它还可以解决您的问题,并处理您现在已完成的任何非托管项
请尝试以下操作:
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
}
}
看起来您可能需要刷新写入程序或关闭写入程序。此外,大多数情况下,您可能希望使用
语句将编写器包装在一个中
幸运的是,在dispose上,它会自动关闭写入程序,刷新要写入的最后一批项,因此它还可以解决您的问题,并处理您现在已完成的任何非托管项
请尝试以下操作:
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
}
}
哦,是的。。。我完全忘了关闭StreamWriter。我可能会使用您推荐的语法。谢谢。哦,是的。。。我完全忘了关闭StreamWriter。我可能会使用您推荐的语法。谢谢,没问题。抱歉,因为我真的应该保证正确回答,而不是随口评论。我提供了一个答案,但只是比Oliver的点击速度慢了一点:-)没问题。抱歉,因为我真的应该保证正确回答,而不是随口评论。我提供了一个答案,但只是比Oliver的点击速度慢了一点:-)