Question

I have a program, that fills textboxes with text. Now i want to save all the text from the textboxes to a file.

So i created this:

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

But when i click the button, my program will exit! I didnt know why.

Someone knows why or is seeing a wrong part in code?

This is what the debug output looks:

'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).

EDIT: thanks for all your answers, but with all the codes, the programs crashes when the savefiledialog1.showdialog(); line was run. I didnt know why. but the other codes dont crash it, just that line of code is wrong. the dialog dont appears and the program forces to exit.

FINAL EDIT (IMPORTANT): Thanks you all guys for helping me and writing codes. but the problem was: not enough RAM to load the savefiledialog. When i run the .exe without visual studio it works fine ;-) thanks everybody for helping me!

Was it helpful?

Solution

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

OTHER TIPS

You must dispose file

file.Dispose();

To flush and close the stream. Run your code in debug mode to get info, it's not clear what is the problem. PS: I assume that "SaveFileDialog1.path" was a typo.

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

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());
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top