1

I have code that works properly reading a csv file as shown:

var lines = File.ReadAllLines("filename.csv").Select(a => a.Split(',')).ToList();

I want to rewrite this statement to read all files in the folder by prefix.

var lines = Directory.GetFiles("prefix*.csv").Select(a => a.Split(',')).ToList();

This code does not work due to the invalid * character in the path name. I know I can do something similar to this with fileinfo.

4

2 回答 2

3

Directory.GetFiles()只是获取文件名,它不读取行。

尝试类似的东西

var lines = Directory.GetFiles(@"c:\", "prefix*.csv").Select(
            fn => File.ReadAllLines(fn).Select(a => a.Split(',')).ToList()).ToList();
于 2013-04-15T21:38:37.717 回答
1

模式匹配必须使用第二个参数,如下所示:

var files = Directory.GetFiles(directory, "prefix*.csv");

于 2013-04-15T21:41:18.280 回答