c# - writing to a text file and reading that text file -


i have used code

 var dest1 = file.appendtext(path.combine(_logfolderpath, "log1.txt"));    dest1.writeline(line.trim()); 

to write text file log1.txt after have read text file...

i have declared in variable... know not possible..but dont know how

using (var file = file.opentext(dest1)) 

how open text file , read file using

while ((line2 = file.readline()) != null) 

any suggestion??

edit:

sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["connectionstring"].connectionstring);             sqldataadapter da = new sqldataadapter("select codesnippet edk_custombrsnippet_vw", con);             datatable dt = new datatable();             da.fill(dt);             string line = dt.rows[0].itemarray[0].tostring().replace("\n", environment.newline).replace("\r", environment.newline);             ;              //messagebox.show(line);              string filepath2 = textbox1.text;             int counter = 1;             string line2;              if (file.exists(filepath2) )             {                 directoryinfo folder = new directoryinfo(textboxpath.text);                 var _logfolderpath = path.combine(textboxpath.text.trim(), "log");                 if (folder.exists)                      if (!directory.exists(_logfolderpath))                         directory.createdirectory(_logfolderpath);                  string filename = path.combine(_logfolderpath, "log1.txt");                 var dest1 = file.appendtext(filename);                      dest1.writeline(line.trim());                  using (var file = file.opentext(filename))                 {                     using (var file2 = file.opentext(filepath2))                     {                         bool time = false;                          while ((line2 = file2.readline()) != null)                         {                             using (var dest = file.appendtext(path.combine(_logfolderpath, "log.txt")))                             {                                 if (!time)                                 {                                     dest.writeline("");                                     dest.writeline("---------------------" + datetime.now + "---------------------");                                     time = true;                                 }                                 bool patternwritten = false;                                 while ((line = file.readline()) != null)                                 {                                     if (line.indexof(line2, stringcomparison.currentcultureignorecase) != -1)                                     {                                         if (!patternwritten)                                         {                                             dest.writeline("");                                             dest.writeline("pattern name : " + line2);                                             patternwritten = true;                                         }                                         dest.writeline("lineno : " + counter.tostring() + " : " + "           " + line.trim());                                     }                                     counter++;                                 }                                 //filepath.basestream.seek(0, seekorigin.begin);                                 counter = 1;                             }                         }                     }                 } 

store filename in variable so:

string filename = path.combine(_logfolderpath, "log1.txt"); 

then use in following lines:

var dest1 = file.appendtext(filename); ... using (var file = file.opentext(filename)) 

then rest should work expected. in code above trying pass streamwriter file.opentext method wrong.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -