c# - Look if file contains a specific string and then read that line -
i using foreach loop search files in directory , read them.
foreach (string file in directory.enumeratefiles(location, "*.mai"))
inside loop want search line in file contains word "sended". there way word , read line?
try it:
var location = @"<your location>"; foreach (string file in directory.enumeratefiles(location, "*.mai")) { var findedlines = file.readalllines(file) .where(l => l.contains("sended")); }
if work big files, should use readlines method, because when use readlines, can start enumerating collection of strings before whole collection returned; when use readalllines, must wait whole array of strings returned before can access array.
another example msdn:
var files = file in directory.enumeratefiles(location, "*.mai") line in file.readlines(file) line.contains("sended") select new { file = file, line = line };
full information, here: https://msdn.microsoft.com/library/dd383503.aspx
Comments
Post a Comment