.net - How to remove all lines in a file, then rewrite the file in Compact Framework 3.5 c# -
in .net framework using windows forms app can purge file, write data want file.
here code use in windows forms:
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 } openfile.close(); openfile.dispose(); i trying same thing compact framework. can keep lines want, , delete lines in file. not able rewrite file.
here compact framework code:
var sb = new stringbuilder(); using (var sr = new streamreader(fullfilename)) { // read first 4 lines nothing them; basically, skip them (int = 0; < 4; i++) sr.readline(); string line1; while ((line1 = sr.readline()) != null) { sb.appendline(line1); } } string allines = sb.tostring(); openfile.close();//close reading of file openfile.dispose(); //reopen file write lines var writer = new streamwriter(fullfilename, false); //don't append! foreach (char line2 in allines) { writer.writeline(line2); } openfile.close();//close rewriting of file } openfile.close(); openfile.dispose();
your code
foreach (char line2 in allines) { writer.writeline(line2); } is writing out characters of original file, each on separate line.
remember, allines single string happens have environment.newline between original strings of file.
what intend simply
writer.writeline(allines); update
you closing openfile number of times (you should once), not flushing or closing writer.
try
using (var writer = new streamwriter(fullfilename, false)) //don't append! { writer.writeline(allines); } to ensure writer disposed , therefore flushed.
Comments
Post a Comment