Writing to a text file on form closing event

2019-09-20 07:48发布

问题:

I am trying to write some strings to a text file on the formclosing event. The problem is that the streamwriter doesn't write anything, it just writes a blank slate. I have 2 different text files, the first one will log all of the graph data and the second text file will log a couple of preferences relevant to my application. My code is shown below for both the closing event and a seperate workhorse method:

  private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {


        if (e.CloseReason.Equals(CloseReason.WindowsShutDown) || (e.CloseReason.Equals(CloseReason.UserClosing))) 
        {
            if (MessageBox.Show("You are closing this application.\n\nAre you sure you wish to exit ?", "Warning: Not Submitted", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Stop) == DialogResult.Yes)
            {

                writeContents("Interrupted");

                return;
            }

            else
                e.Cancel = true; 
        } 



    }

    private void writeContents(string status)
    {

        //---writes the graph data-----
        TextWriter twBackupData = new StreamWriter("C://springTestBackupData.txt");

        twBackupData.WriteLine("--Cycle#-- --TorqueLower-- --TorqueUpper--");

        //writes the table of values in there, assume x and y are the same size arrays
        for(int i = 0; i < x.Count; i++)
        {               
            twBackupData.WriteLine(x[i] + "   " + y_lower[i] + "   " + y_upper[i]);
        }


        //---writes some of the preferences------
        TextWriter twBackupDataInfo = new StreamWriter("C://springTestBackupInfo.txt");

        twBackupDataInfo.WriteLine(status);
        twBackupDataInfo.WriteLine(cycleCount.ToString());
        twBackupDataInfo.WriteLine(section.ToString());
        twBackupDataInfo.WriteLine(revsPerCycle.ToString());
        twBackupDataInfo.WriteLine(preturns.ToString());
        twBackupDataInfo.WriteLine(direction.ToString());

    }

If you can provide advice or help me find out why it's writing blanks I would greatly appreciate it. Thank you!

回答1:

You need to close the StreamWriter using the using statement.



回答2:

It's much easier to just use:

var linesToWrite = new list<string>();

linesToWrite.Add(status);
linesToWrite.Add(cycleCount.ToString());
...

File.WriteAllLines("C://springTestBackupData.txt", linesToWrite);


回答3:

You need to close/dispose the writer for it to write, otherwise it never flushes its stream (i.e. writes the data to the file)

Using the 'using' statement automatically disposes of an object when it goes out of scope so:

using(TextWriter twBackupData = new StreamWriter("C://springTestBackupData.txt"))
{
     // Do your stuff here - write to the tw ---


    twBackupData.WriteLine("--Cycle#-- --TorqueLower-- --TorqueUpper--");   

    //writes the table of values in there, assume x and y are the same size arrays   
    for(int i = 0; i < x.Count; i++)   
    {                  
        twBackupData.WriteLine(x[i] + "   " + y_lower[i] + "   " + y_upper[i]);   
    }   
}

Will ensure your file gets written to

More info here:

http://msdn.microsoft.com/en-us/library/yh598w02.aspx



回答4:

You need to do .Close() on your StreamWriters;



标签: c# io text-files