HI
i am trying to save my information to a file, i have looked at various examples which say the following should work
i pass the filename i need throught the savedialog box, and have tried changingCode:Stream myStream; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { if ((myStream = saveFileDialog1.OpenFile()) != null) { try { StreamWriter wText = new StreamWriter(myStream); wText.Write("Test Text"); // Code to write the stream goes here. } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } myStream.Close(); } }
StreamWriter wText = new StreamWriter(myStream);
to
StreamWriter wText = new StreamWriter(saveFileDialog1.Filename); but it throughs an exception as the stream is already open
i have tried changing the wText.Write to wText.WriteLine but that didnt help.
this creates the file ok but the file is empty - i.e. my "Test Text" is not written to the created file.
am i missing something here or should this work?
i eventually need to write out a List<> to this file and be able to read it back in again.
im guessing i am going to have to delimit the file in some way for when i read it back in as my List<> contents will be varying length ?
for the moment if i can just get it to put that text in the file i can move forward on testing the rest.
many thanks