mwmosu

mwmosu

  • NA
  • 10
  • 0

delete specified lines in a .txt file

Jun 4 2004 1:42 PM
I am trying to loop through a .txt or .doc file in Word and delete specified lines. Here is the code I have so far: try { FileStream fs = new FileStream(tmpdir + newfilename , FileMode.OpenOrCreate, FileAccess.Write); // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (StreamReader sr = new StreamReader(fs)) { MessageBox.Show("inside using"); //this shows up StreamWriter sw = new StreamWriter(fs); //this line makes the code skip the rest of the code shown. MessageBox.Show("before while"); //this doesn't show up, and nothing else executes after this. string line = ""; string sentinel = "start"; StringBuilder bu = new StringBuilder(line); // Read lines from the file until the end of the file //is reached. while((line = sr.ReadLine()) != null) { if (line.IndexOf("GOTO9988") == -1) { sentinel = "stop"; } else if (line.IndexOf("<9988>") == -1) { sentinel = "start"; } if (sentinel == "start") { bu.Remove(0,line.Length); } } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } Any help would be appreciated.