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();

Recommended Answers

All 2 Replies

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.

U the man

it worked

Thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.