I am trying to add a SaveFileDialog to my Windows Application, in which I save a simple text file from the contents of a multi-line text box. The program seems to work fine up until the save dialog, and I don't get any errors, but once I click the save button the application just hangs. The only way out of it is to click the "stop-debugging" button. Here are the code sections that I have tried, but both create the same hang:
private void button_SaveToFile_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.FileName, textBox_ListDestination.Text);
}
}
and
private void button_SaveToFile_Click(object sender, EventArgs e)
{
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)
{
File.WriteAllText(saveFileDialog1.FileName, textBox_ListDestination.Text);
myStream.Close();
}
}
}
Jeagr,
I don't have any issues when I try to recreate the problem using your first example code. If I were to guess, you may be having problems with memory allocation (working with unmanaged resources) coupled with multiple attempts at debugging, and maybe re-saving to the same file over and over may have left a bad file pointer.
You second example will not work. When you call File.WriteAllText, it opens, writes and closes the file for you automatically. (Read here: http://msdn.microsoft.com/en-us/library/system.io.file.writealltext.aspx)
When you call OpenFile, you are placing a lock on that file. In your code, when File.WriteAllText is executed, it blows up because the file is already in use. If you want to work with your file using OpenFile, then you will have to change how you work with the file. Here is an example: http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.openfile(v=VS.71).aspx Note how the example closes the file, if you wanted to do working in the file, you would code that work before the CloseFile method is called.
If you would like an example on how to work with a file using OpenFile, there should be a couple of examples on the internet, but based on your need, I think File.WriteAllText will work.
Back to the first example.
If you are hitting the code multiple times during debugging, my only suggestion would be to wrap the SaveFileDialog in a 'using' statement. This may help with the system hanging, and some debugging.
private void button1_Click(object sender, EventArgs e)
{
using (var sfd = new SaveFileDialog())
{
sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
sfd.FilterIndex = 2;
if (sfd.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(sfd.FileName, textBox_ListDestination.Text);
}
}
}