none
textWriter. streamWrite, and fileStream - append RRS feed

  • Question

  • i'm trying to append to an output file"test1.txt"  but when i try the data from first output is written over.  i've made some changes and now
     it says the 
    file is in use.  i see why but i can't figure out how to append correctly.   can someone one please show me
    how to append?

      FileStream FS = File.Open(@"C:\Users\frank\Documents\test1.txt", FileMode.Append, FileAccess.Write);  
              TextWriter TW ;
    
       TW = new StreamWriter(@"C:\Users\frank\Documents\test1.txt");
                TW.WriteLine( "{0},{1},{2},{3}", name,today, answersCorrect,percentCorrect );
                TW.Close();
    

    new to this - vs 2008, ult 64
    Tuesday, May 26, 2009 3:08 PM

Answers

  • Don't instantiate the StreamWriter with the string path.  Instantiate it by passing in the FileStream, FS. 

    TW = new StreamWriter(FS);

    This will prevent the file in use error as well making sure you append.


    David Morton - http://blog.davemorton.net/ - @davidmmorton
    • Marked as answer by ofranko Tuesday, May 26, 2009 3:26 PM
    Tuesday, May 26, 2009 3:14 PM
    Moderator