0

我有一个程序,用文本填充文本框。现在我想将文本框中的所有文本保存到文件中。

所以我创建了这个:

    private void button3_Click(object sender, EventArgs e)
    {
        timer1.Stop();
        saveFileDialog1.ShowDialog();
        System.IO.StreamWriter file = new System.IO.StreamWriter("SaveFileDialog1.path");
        file.WriteLine(" Time and date:\r\n " + textBox2.Text + "\r\n \r\n Memory:\r\n " + textBox1.Text + "\r\n " + textBox10.Text + "\r\n " + textBox4.Text + "\r\n " + textBox5.Text + "\r\n \r\n CPU:\r\n " + textBox6.Text + "\r\n " + textBox11.Text + "\r\n " + textBox12.Text + "\r\n " + textBox13.Text + "\r\n \r\n Network:\r\n " + textBox7.Text + "\r\n " + textBox3.Text + "\r\n " + textBox8.Text + "\r\n " + textBox9.Text + "\r\n ");
        timer1.Start();
    }

但是当我单击按钮时,我的程序将退出!我不知道为什么。

有人知道为什么或看到代码中有错误的部分吗?

这是调试输出的样子:

'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'd:\documents\visual studio 2012\Projects\SystemControl\SystemControl\bin\Debug\SystemControl.vshost.exe', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Deployment\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Deployment.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 'vshost.NotifyLoad' (0x1dc8) has exited with code 0 (0x0).
The thread '<No Name>' (0x162c) has exited with code 0 (0x0).
The thread 'vshost.LoadReference' (0x19cc) has exited with code 0 (0x0).
'SystemControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'd:\documents\visual studio 2012\Projects\SystemControl\SystemControl\bin\Debug\SystemControl.exe', Symbols loaded.
The program '[5560] SystemControl.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[5560] SystemControl.vshost.exe: Managed (v4.0.30319)' has exited with code -2147483645 (0x80000003).

编辑:感谢您的所有回答,但是对于所有代码,程序在 savefiledialog1.showdialog(); 时崩溃;行已运行。我不知道为什么。但其他代码不会崩溃,只是那行代码是错误的。对话框不出现,程序强制退出。

最终编辑(重要):感谢大家帮助我和编写代码。但问题是:没有足够的 RAM 来加载 savefiledialog。当我在没有 Visual Studio 的情况下运行 .exe 时,它​​工作正常;-) 感谢大家帮助我!

4

4 回答 4

2

I don't know what the timer is used for, but here is corrected code (file name and path passed to StreamWriter was obviously bad):

private void button3_Click(object sender, EventArgs e)
{
    timer1.Stop();
    if(saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        System.IO.StreamWriter file = new System.IO.StreamWriter(saveFileDialog1.FileName);
        file.WriteLine(" Time and date:\r\n " + textBox2.Text + "\r\n \r\n Memory:\r\n " + textBox1.Text + "\r\n " + textBox10.Text + "\r\n " + textBox4.Text + "\r\n " + textBox5.Text + "\r\n \r\n CPU:\r\n " + textBox6.Text + "\r\n " + textBox11.Text + "\r\n " + textBox12.Text + "\r\n " + textBox13.Text + "\r\n \r\n Network:\r\n " + textBox7.Text + "\r\n " + textBox3.Text + "\r\n " + textBox8.Text + "\r\n " + textBox9.Text + "\r\n ");
        file.Close();
    }
    timer1.Start();
}
于 2013-08-20T17:26:01.043 回答
0

您必须处置文件

file.Dispose();

刷新和关闭流。在调试模式下运行您的代码以获取信息,目前尚不清楚是什么问题。PS:我认为“SaveFileDialog1.path”是一个错字。

于 2013-08-20T17:28:44.727 回答
0
//Typically you wrap your streamwriter in a using statement but you can just do this instead.
Stream myStream ;
 SaveFileDialog saveFileDialog1 = new SaveFileDialog();

 saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
 saveFileDialog1.FilterIndex = 2 ;
 saveFileDialog1.RestoreDirectory = true ;

 if(saveFileDialog1.ShowDialog() == DialogResult.OK)
 {
     if((myStream = saveFileDialog1.OpenFile()) != null)
     {
         // Code to write the stream goes here.
         myStream.Close();
     }
 }
于 2013-08-20T17:28:51.557 回答
0

Do it like this and as your code needed a lot of refactoring so i have done that as well. done.

private void button3_Click(object sender, EventArgs e)
    {
       try
        {
            timer1.Stop();
            if(saveFileDialog1.ShowDialog()== DialogResult.Ok)
            {
               using(System.IO.StreamWriter file = new System.IO.StreamWriter(saveFileDialog1.FileName))
               {
                   StringBuilder sb = new StringBuilder(" Time and date:\r\n ");
                   sb.Append(Environment.NewLine);
                   sb.Append(textBox2.Text);
                   sb.Append(Environment.NewLine);
                   sb.Append(Environment.NewLine);
                   sb.Append(" Memory:");
                   sb.Append(Environment.NewLine);
                   sb.Append(textBox1.Text);
                   sb.Append(Environment.NewLine);
                   sb.Append(textBox10.Text);
                   ........

                   file.WriteLine(sb.ToString());
              }
           }
           timer1.Start();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
于 2013-08-20T17:25:48.937 回答