c# - How to skip lines while reading in a file using compact framework 3.5 -
i have windows form app have rewritten using compact framework 3.5. in original app had block of code used read in file , skip first 4 lines.
here code block works fine:
var openfile = file.opentext(fullfilename); var fileempty = openfile.readline(); if (fileempty != null) { var lines = file.readalllines(fullfilename).skip(4); //will skip first 4 rewrite file openfile.close();//close reading of file file.writealllines(fullfilename, lines); //reopen file write lines openfile.close();//close rewriting of file } i had rewrite above code in since can't used in compact framework.
here code:
var openfile = file.opentext(fullfilename); var fileempty = openfile.readline(); if (fileempty != null) { var sb = new stringbuilder(); using (var sr = new streamreader(fullfilename)) { string line1; // read , display lines file until end of // file reached. while ((line1 = sr.readline().skip(4).tostring()) != null) //will skip first 4 rewrite file { sb.appendline(line1); } } however when run above, receive error on (while ((line1 = sr.readline().skip(4).tostring()) != null)) argumentnullexception unhandled , value can not null.
can please tell me how can in compact framework?
since sr.readline() returns single string, going skip first 4 characters in string, return rest character array, , call tostring() on it... not want.
sr.readline().skip(4).tostring() the reason you're getting argumentnullexception because sr.readline() returns null string, , when try skip first 4 characters of null string, throws, can see looking @ implementation skip():
public static ienumerable<tsource> skip<tsource>(this ienumerable<tsource> source, int count) { if (source == null) throw new argumentnullexception("source"); return skipiterator<tsource>(source, count); } keeping of code same, read first few lines , nothing them (assuming you'll have @ least 4 lines in file).
using (var sr = new streamreader(fullfilename)) { // read first 4 lines nothing them; basically, skip them (int i=0; i<4; i++) sr.readline(); string line1; while ((line1 = sr.readline()) != null) { sb.appendline(line1); } }
Comments
Post a Comment