About saveFileDialog

T

Tony Johansson

Hello!

I have a TextBox control in my program which is called txtBox.
When I write something in the TextBox control and click on the SaveAsMeny
item this
event handler SaveAsToolStripMenuItem_Click is called.
My problem is that nothing will be written to the filename that was chosen
in the saveFileDialog.

Does anybody have any idea why the file is empty after having written the
contens in the TextBox to the file.
This is done by using
new StreamWriter(fs).Write(txtBox.Text);
in the SaveFile method shown below.

private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
SaveFile(saveFileDialog.FileName);
}
}

private void SaveFile(string fileName)
{
try
{
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
new StreamWriter(fs).Write(txtBox.Text);
}
}
catch (Exception)
{
}
}

//Tony
 
C

Cor Ligthert[MVP]

private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
SaveFile(saveFileDialog.FileName);
}
}

private void SaveFile(string fileName)
{
try
{
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
new StreamWriter(fs).Write(txtBox.Text);
}
}
catch (Exception)
{
}
}
The dirtiest code I have seen for years, it looks like the old VB6 times
when we saw some persons do Resume OnError which was never trapped and then
were surprised it gave not wanted results.

Probably as you make from the catch something as

catch (Exception ex)
{
MessageBox.Show(ex.ToString();)
}
Then you see it probably yourself. I did not really look at the code.



Cor
 
J

Jeff Johnson

private void SaveFile(string fileName)
{
try
{
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
new StreamWriter(fs).Write(txtBox.Text);
}
}
catch (Exception)
{
}
}

1) Use Cor's suggestion of writing out the error message or set up the IDE
to break on all errors.

2) Why not just use the quick way:

try
{
File.WriteAllText(fileName, txtBox.Text);
}
 
T

Tim Roberts

Tony Johansson said:
Does anybody have any idea why the file is empty after having written the
contens in the TextBox to the file.
This is done by using
new StreamWriter(fs).Write(txtBox.Text);
in the SaveFile method shown below.
...
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
new StreamWriter(fs).Write(txtBox.Text);
}
}

Poor style has given you the worst possible timing. By using the "using"
clause with the outer object but not the inner object, you are forcing the
FileStream to be closed and disposed immediately, whereas the StreamWriter
instance will be closed and disposed lazily. As it turns out, this causes
the FileStream to be closed before the StreamWriter has flushed its buffer.

For example, this slight change will work, although you still have some
style issues:

using (FileStream fs = new FileStream("abc.txt", FileMode.Create ))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write( txtBox.Text );
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top