0

I use this code to save my xml file.

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.DefaultExt = ".FFDATA";
saveFileDialog1.Filter = "Form|*.FFDATA";
saveFileDialog1.FileName = "A_"+code;//here code is a generated number, always unique

Stream myStream;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    if ((myStream = saveFileDialog1.OpenFile()) != null)
    {
        StreamWriter wText = new StreamWriter(myStream);
        wText.Write(result.ToString());

        myStream.Close();
    }
}

However when I open the file, the text inside is cut in some place and the second part of it is missing. (I breakpointed wText.Write(result.ToString()); and result.ToString() is as I expect - a normal text.

Then I tried putting wText.Write("Test?"); and nothing got saved into a file, however the file itself appeared.

So no matter how short the text I am trying to save, I get at least some data missing.

Question: what am I doing wrong?

P.S. I found this example and modified slightly to suit my needs.

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78

5 Answers5

4

The contents are probably not flushed. This is easily avoided by calling Flush yourself or using a 'using' block which also handles disposing/closing etc.

using (StreamWriter wText = new StreamWriter(myStream))
{
  wText.Write(result.ToString());
  //wText.Flush(); //this should not be needed because close will flush
}

Note the absence of any .Close() here... disposing the streamreader via the using block takes care of that for you, and makes sure it's done correctly.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
RvdK
  • 19,580
  • 4
  • 64
  • 107
2

Call flush() on the StreamWriter before close(), this will flush the output buffer to file. Or call close on the streamwriter directly, this should flush the writer and close the underlying stream. If you want, you can just set AutoFlush to true, this could have an impact on performance.

Jeff Watkins
  • 6,343
  • 16
  • 19
0

Make sure you close the StreamWriter wText. You're closing the underlying stream, but not the StreamWriter that is writing to your file. Calling StreamWriter.Close() will also close the underlying stream for you. Make sure you use try/finally to ensure that your streams get closed in the event of an exception (such as an IOException). The following code should solve your issues:

SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
saveFileDialog1.DefaultExt = ".FFDATA"; 
saveFileDialog1.Filter = "Form|*.FFDATA"; 
saveFileDialog1.FileName = "A_"+code;//here code is a generated number, always unique 

Stream myStream;

if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
{
    if ((myStream = saveFileDialog1.OpenFile()) != null) 
    {
        StreamWriter wText = new StreamWriter(myStream);
        try
        { 
            wText.Write(result.ToString()); 
        }
        // Close Stream and StreamWriter in a finally block so that they are
        // closed even if an exception occurs.
        finally
        {
            // StreamWriter.Close() will close the underlying Stream for you
            wText.Close();
        }
    } 
} 
Jon Senchyna
  • 7,867
  • 2
  • 26
  • 46
0

Have you tried this alternate approach ?

File.AppendAllText(path, result.ToString())
Larry
  • 17,605
  • 9
  • 77
  • 106
0

Try:

    StreamWriter wText = new StreamWriter(myStream); 
    wText.Write(result.ToString()); 
    wText.Close();
    myStream.Close(); 

Or(better):

using (var myStream = saveFileDialog1.OpenFile())
{
  if (myStream != null)    
  {    
    using (var wText = new StreamWriter(myStream))
    {    
      wText.Write(result.ToString());    
    }
  }
}    
Serj-Tm
  • 16,581
  • 4
  • 54
  • 61