C# 表单关闭事件时写入文本文件

C# 表单关闭事件时写入文本文件,c#,io,text-files,C#,Io,Text Files,我试图在formclosing事件中将一些字符串写入文本文件。问题是streamwriter什么都不写,只是写了一张白纸。我有两个不同的文本文件,第一个将记录所有图形数据,第二个文本文件将记录与我的应用程序相关的两个首选项。下面显示了关闭事件和单独的workhorse方法的代码: private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason.Equ

我试图在formclosing事件中将一些字符串写入文本文件。问题是streamwriter什么都不写,只是写了一张白纸。我有两个不同的文本文件,第一个将记录所有图形数据,第二个文本文件将记录与我的应用程序相关的两个首选项。下面显示了关闭事件和单独的workhorse方法的代码:

  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());

    }
private void Form1\u FormClosing(对象发送方,FormClosingEventArgs e)
{
如果(e.CloseReason.Equals(CloseReason.WindowsShutDown)| |(e.CloseReason.Equals(CloseReason.UserClosing)))
{
if(MessageBox.Show(“您正在关闭此应用程序。\n\n确实要退出吗?”,“警告:未提交”,MessageBoxButtons.YesNoCancel,MessageBoxIcon.Stop)=DialogResult.Yes)
{
书面内容(“中断”);
返回;
}
其他的
e、 取消=真;
} 
}
私有void writeContents(字符串状态)
{
//---写入图形数据-----
TextWriter twbackupdatea=newstreamwriter(“C://springtestbackupdatea.txt”);
twbackupdatea.WriteLine(“--Cycle#----TorqueLower--TorqueLower--TorqueUpper--”;
//将值表写入其中,假设x和y是相同大小的数组
对于(int i=0;i

如果你能提供建议或帮助我找出为什么写空白,我将不胜感激。谢谢大家!

您需要使用
using
语句关闭
StreamWriter

您需要使用
using
语句关闭
StreamWriter

您需要对StreamWriter执行
关闭()

您需要在StreamWriter上执行
.Close()

只需使用它就容易多了:

var linesToWrite = new list<string>();

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

File.WriteAllLines("C://springTestBackupData.txt", linesToWrite);
var linesToWrite=new list();
linesToWrite.Add(状态);
linesToWrite.Add(cycleCount.ToString());
...
writeAllines(“C://springtestbackupdatea.txt”,linesToWrite);

只需使用它就更容易了:

var linesToWrite = new list<string>();

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

File.WriteAllLines("C://springTestBackupData.txt", linesToWrite);
var linesToWrite=new list();
linesToWrite.Add(状态);
linesToWrite.Add(cycleCount.ToString());
...
writeAllines(“C://springtestbackupdatea.txt”,linesToWrite);

您需要关闭/处理writer以便写入,否则它永远不会刷新其流(即将数据写入文件)

当对象超出范围时,使用“Using”语句会自动处理对象,因此:

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]);   
    }   
}
使用(TextWriter twbackupdatea=newstreamwriter(“C://springtestbackupdatea.txt”))
{
//在这里做你的事情-写信给tw---
twbackupdatea.WriteLine(“--Cycle#----TorqueLower--TorqueLower--TorqueUpper--”;
//将值表写入其中,假设x和y是相同大小的数组
对于(int i=0;i
将确保您的文件被写入

更多信息请点击此处:


您需要关闭/处理writer以便写入,否则它永远不会刷新其流(即,将数据写入文件)

当对象超出范围时,使用“Using”语句会自动处理对象,因此:

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]);   
    }   
}
使用(TextWriter twbackupdatea=newstreamwriter(“C://springtestbackupdatea.txt”))
{
//在这里做你的事情-写信给tw---
twbackupdatea.WriteLine(“--Cycle#----TorqueLower--TorqueLower--TorqueUpper--”;
//将值表写入其中,假设x和y是相同大小的数组
对于(int i=0;i
将确保您的文件被写入

更多信息请点击此处:


尝试StreamWriter.Flush()然后是StreamWriter.Close(),我想
.Close
实际上也会调用
.Flush
。尝试StreamWriter.Flush()然后是StreamWriter.Close(),我想
.Close
实际上也会调用
.Flush