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

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -