c# - How to delete entry from file -
try delete entry csv file, if know part of string code
//open file filestream fs = new filestream(filepath, filemode.open, fileaccess.readwrite); streamreader sr = new streamreader(fs); //divide file entry separate lines string []line = sr.readtoend().split(new string[] { environment.newline }, stringsplitoptions.removeemptyentries); // find required entry , create new list without list<string> finaldata = new list<string>(); foreach (var l in line) { if (!line.contains((datetimepicker1.text.tostring().trim() + ','+ eventnamedeletetextbox.text.tostring().trim()+','))) finaldata.add(l); } // convert array string tocsvoutput = string.join(environment.newline, finaldata.toarray()); // viewtextbox1.text=tocsvoutput; //updatefile sr.close(); filestream fs1 = new filestream(filepath, filemode.open,fileaccess.write, fileshare.none); streamwriter sw = new streamwriter(fs1); sw.writeline(tocsvoutput); sw.close();
as result - nothing heppend, i'm wrong?
replace line l:
if (!l.contains((datetimepicker1.text.tostring().trim() + ','+ eventnamedeletetextbox.text.tostring().trim()+','))) finaldata.add(l);
Comments
Post a Comment