我想要写一个大约一千万行的文件。我使用的是StreamWriter和using语句,但是似乎StreamWriter没有刷新。这是我的代码:
public void ExportRecords(IEnumerable<Record> records, string path)
{
using(TextWriter writer = new StreamWriter(path))
{
writer.WriteLine("header");
foreach(var record in records)
{
string line = "";
//Fill line with record properties
writer.WriteLine(line);
}
}
}在某一行,StreamWriter不添加任何行。那么问题在哪里呢?
编辑
如前所述,代码是运行异步的注释。
情况如下:
Task.Run(() =>
{
if (!String.IsNullOrEmpty(folderPath))
{
var records = _generator.GenerateData();
_dataService.ExportRecords(records, $"{folderPath}/RECORDS.csv");
//Just a message box
_reporter.ReportMessage($"Generation and exportation finished");
}
else
{
IsGenerating = false;
_reporter.ReportMessage("No output file was created");
}
});因此,我确实设置了一个断点来检查该方法是否开始执行,它确实执行了。我只是表示代码没有卡在生成方法中。
https://stackoverflow.com/questions/55550019
复制相似问题