May 13, 2014

Searching for text within files using Linq

A good example of the power of Linq:
Directory.EnumerateFiles(@"S\Source\", "*.*proj", SearchOption.AllDirectories)
    .SelectMany(file => File.ReadAllLines(file).Select((text,n)=> new {text,lineNumber=n+1,file}))
    .Where(line => Regex.IsMatch(line.text,@"SccProjectName"))
    .Where(line => !Regex.IsMatch(line.text,@"SAK"))
    .Dump("Should be set to SAK")