1 行:
using System.IO;
using System.Linq;
var result = File.ReadAllLines(@"c:\temp").Select(s => s.Contains("eng"));
または、よりメモリ効率の高いソリューションが必要な場合は、拡張メソッドをロールすることができます。FileInfo
、FileStream
などをベース ハンドラとして使用できます。
public static IEnumerable<string> ReadAndFilter(this FileInfo info, Predicate<string> condition)
{
string line;
using (var reader = new StreamReader(info.FullName))
{
while ((line = reader.ReadLine()) != null)
{
if (condition(line))
{
yield return line;
}
}
}
}
使用法:
var result = new FileInfo(path).ReadAndFilter(s => s.Contains("eng"));