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.
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.
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.