c# - Read previous line using File.ReadLines -
this question has answer here:
i want read text file last line because big size , want check today's log content.
i'm using file.readlines
() , success read last line. how can read previous line? or there way current line number can subtract number?
foreach (string f in directory.getfiles(sdir, "*.log", searchoption.alldirectories)) { string last = file.readlines(f, encoding.default).last(); //read last line here if (last.indexof("error", stringcomparison.ordinalignorecase) > 0) { writeerror(last); } }
foreach (string f in directory.getfiles(sdir, "*.log", searchoption.alldirectories)) { string last="", prev=""; foreach (string cur in file.readlines(f, encoding.default) ) { prev = last; last = cur; } //not sure want "prev" here. if (last.indexof("error", stringcomparison.ordinalignorecase) > 0) { writeerror(last); } }
Comments
Post a Comment