visual studio 2010 - Searching for a string in file using c# -
i wanna search string(function) in large file , if found, have search string(signal) , replace it. have written code signal getting updated outside function. want modify signals found inside function. iam beginner c# , kind of appreciated.
if (openfiledialog2.filename.contains(function)) file.writealltext(openfiledialog2.filename, file.readalltext(openfiledialog2.filename).replace(signal, replace)); messagebox.show("done"); }
i tried,
string contents = file.readalltext(openfiledialog2.filename); if(contents.contains(function)) { file.writealltext(openfiledialog2.filename, file.readalltext(openfiledialog2.filename).replace(signal, replace)); messagebox.show("done"); }
and this,
using (var reader = new streamreader(openfiledialog2.filename)) { string currentline; if ((currentline = reader.readline()) != null) { while (currentline.contains(function)) { file.writealltext(openfiledialog2.filename, file.readalltext(openfiledialog2.filename).replace(signal, replace)); messagebox.show("done"); }}}
but nothing seems work. if code works without errors, signals outside function updated.
in second example seems replace after writing new content, try this:
string contents = file.readalltext(openfiledialog2.filename); if (contents.contains(function)) { file.writealltext(openfiledialog2.filename, contents.replace(signal, replace)); messagebox.show("done"); }
here replace before writing edited content read
Comments
Post a Comment