为什么我的“保存”按钮不起作用?

时间:2013-08-20 17:19:30

标签: c# button crash savefiledialog

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

所以我创造了这个:

    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 个答案:

答案 0 :(得分:2)

我不知道计时器的用途是什么,但这里有更正的代码(传递给StreamWriter的文件名和路径显然很糟糕):

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

答案 1 :(得分:0)

这样做,因为你的代码需要大量的重构,所以我也这样做了。完成。

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

答案 2 :(得分:0)

您必须处置文件

file.Dispose();

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

答案 3 :(得分: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();
     }
 }