Append records to existing StreamWriter file

I've been writing text files in several apps with no problems until I tried to write/append to a file. Now, from what I could 'glean' from the numerous books/websites out there, to append records to an existing file one needs to set the StreamWriter to true like this: m_objOutput = new StreamWriter( strFileName, true );

The output file gets created; however, when I run my app, nothing is written to the file. I haven't been able to find anything wrong/different in my code from the many examples I've found. Can someone please show me what I am/not not doing

private void btnOpenFile_Click(object sender, System.EventArgs e)
{
//* display the Open Dialog
DialogResult result = openFileDialog1.ShowDialog();

//* if the user pressed cancel, then get out of here
//* and reset the buttons
if ( result == DialogResult.Cancel )
{
return;
}

//* get the specified file name
string strFileName = openFileDialog1.FileName;

//* Open the output file with append true
if ( CheckValidity( strFileName ) == true )
{
m_objOutput = new StreamWriter( strFileName, true );
}
}

private void btnEnter_Click(object sender, System.EventArgs e)
{
m_objOutput.WriteLine( this.updDay.Value.ToString() );
m_objOutput.WriteLine( this.dtpTime.Text );
m_objOutput.WriteLine( this.txtPrice.Text );
m_objOutput.WriteLine( this.txtEvent.Text );
m_objOutput.WriteLine( this.txtDescription.Text );
} //* end method btnEnter_Click()



Answer this question

Append records to existing StreamWriter file

  • Michela

    I don't have the compiler in front of me, but you might need to add this line:

    m_objOutput.Flush();

    ... to the end of your btnEnter_Click method after the last WriteLine call... (Also, make sure to close that file!)



  • RazzleDazzle

    That did it! Thank you iTIE! I must've been staring at this code for too long, geez, I should've seen that.

  • Hamish_NZ

    NP, glad to help! :D

  • Append records to existing StreamWriter file