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

本文关键字:不起作用 按钮 保存 为什么 我的 | 更新日期: 2023-09-27 18:28:26

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

所以我创建了这个:

    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时,它运行得很好;-)谢谢大家帮助我!

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

我不知道计时器是用来做什么的,但这里有更正的代码(传递给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();
}

您必须处理文件

file.Dispose();

冲洗并关闭溪流。在调试模式下运行代码以获取信息,目前尚不清楚问题出在哪里。PS:我认为"SaveFileDialog1.path"是一个拼写错误。

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

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

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