0

i am having problems appending txt to a file. it overwrites the file instead of appending

private StreamWriter outputfile = new StreamWriter("c:\\testfile.txt");

public void SetFile()
        {
            this.outputfile.WriteLine("Test",FileMode.Append, FileAccess.Write);
            this.outputfile.Close();
        }

-----------------------------------------------------------------
main class

FileRead Testing1 = new FileRead();

            Testing1.SetFile();
2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by mrjoli021
0

Hi,

I think There is no method of StreamWriter.WriteLine ( string, FileMode, FileAccess);

But you can open File in Append Mode in Constructor
StreamWriter ( string file, bool Append) ;

You Change the code as

//Append Mode
[B]private StreamWriter outputfile = new StreamWriter("c:\\testfile.txt", true);[/B]

public void SetFile() {
     this.outputfile.WriteLine("Test");
     this.outputfile.Close();
}

-----------------------------------------------------------------
main class
  FileRead Testing1 = new FileRead();
  Testing1.SetFile();

Its working good.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.