我正在接收来自目录的所有.csv文件,从每个使用CSVhelper的文件中取出几列,然后写入新的.csv文件。但是,当我试图接收文件时,它似乎只采取目录中的最后一个文件,我无法理解为什么。我把文件中是这样的:使用CSVhelper接收多个.csv文件
static void Main(string[] args)
{
string sourceDirectory = @"C:\Users\SourceDirectory";
var csvFiles = Directory.EnumerateFiles(sourceDirectory, "*.csv", SearchOption.AllDirectories);
foreach (string currentFile in csvFiles)
{
readFile(currentFile);
}
}
,然后执行这些文件中的变化,例如:
public static void readFile(string currentFile)
{
using (var sr = new StreamReader(currentFile))
{
using (var sw = new StreamWriter(@"C:\Users\newFile.csv"))
{
var reader = new CsvReader(sr);
var writer = new CsvWriter(sw);
//CSVReader will now read the whole file into an enumerable
IEnumerable dataRecord = reader.GetRecords<dataRecord>().ToList();
foreach (dataRecord record in dataRecord)
{
//Choose which data values you want to keep
writer.WriteField(record.info1);
writer.WriteField(record.info2);
writer.WriteField(record.info3);
//Moves the pointer onto the next record
writer.NextRecord();
}
我不明白为什么它不把所有的文件和更糊涂了至于为什么只采取最后?
难道它会通过你的所有的源文件,但你的代码是在结束它覆盖每次输出文件,所以只包含最后一个的输出。 – Andy