SaveFileDialog bug?

  • Thread starter Thread starter Neville
  • Start date Start date
N

Neville

SaveFileDialog's ShowDialog method returns
DialogResult.Cancel when a user clicks the "Yes" button
when asked whether to overwrite an existing file. Is
this a known SaveFileDialogBug?

I'm running version 1.1.4322.573 of the .Net Framework
on "Windows XP Professional 2002 Service Pack 1".

Sample code illustrating the problem is shown below. The
contents of foo.txt are never "replacement text".
---------------------------------------------------

using System;
using System.IO;
using System.Windows.Forms;

namespace Frustration
{
class Sample
{
[STAThread]
static void Main(string[] args)
{
SaveFile ("foo.txt", "original text");
SaveFile ("foo.txt", "replacement text");
}

// Shows a SaveFileDialog prompting the user to
// save the specified file. Attempts to write
// the specified text
// to the file.
static void SaveFile (string fileName,
string text)
{
SaveFileDialog saveFileDialog =
new SaveFileDialog();

saveFileDialog.FileName = "foo.txt";

// OverwritePrompt is true by default, but I
// explicitly set it for clarity.

saveFileDialog.OverwritePrompt = true;

if (saveFileDialog.ShowDialog() !=
DialogResult.Cancel)
{
StreamWriter writer = null;
try
{
writer = new StreamWriter
(saveFileDialog.OpenFile());

writer.Write (text);
writer.Flush();
}
finally
{
writer.Close();
}
} // !Cancel
} // SaveFile
} // Sample
} // Frustration
 
I built a test project, showed a save dialog in response to a button click,
selected an existing file, said Yes I want to overwrite, and it returned
DialogResult.OK. Then I pasted your code, and it failed exactly as you
describe, incorrectly returning DialogResult.Cancel when I try to overwrite
an existing file. I spent a while puzzling over what was different between
the two test projects, and I think I've found it.

The difference is that I was showing the dialog in response to a button
click. So Application.Run() was already running in my test (the one that
worked). But in your test, you were showing the dialog *before* you called
Application.Run(). And that seems to make all the difference. If I move
the contents of your Main() method (the two calls to SaveFile()) into a
button-click event, then everything starts working as expected.

It's possible (probable?) that some part of the common-controls library
doesn't get properly initialized until Application.Run() is called for the
first time, and that's why you're getting the occasional wrong DialogResult.
If you show the save dialog from inside your main UI -- in response to a
menu item or a button click or some such, while Application.Run() is in
effect -- then there's a fair chance that it will work just fine.

Sounds like a bug to me too, but a bug you can work around is a lot better
than some of the alternatives...


Neville said:
SaveFileDialog's ShowDialog method returns
DialogResult.Cancel when a user clicks the "Yes" button
when asked whether to overwrite an existing file. Is
this a known SaveFileDialogBug?

I'm running version 1.1.4322.573 of the .Net Framework
on "Windows XP Professional 2002 Service Pack 1".

Sample code illustrating the problem is shown below. The
contents of foo.txt are never "replacement text".
---------------------------------------------------

using System;
using System.IO;
using System.Windows.Forms;

namespace Frustration
{
class Sample
{
[STAThread]
static void Main(string[] args)
{
SaveFile ("foo.txt", "original text");
SaveFile ("foo.txt", "replacement text");
}

// Shows a SaveFileDialog prompting the user to
// save the specified file. Attempts to write
// the specified text
// to the file.
static void SaveFile (string fileName,
string text)
{
SaveFileDialog saveFileDialog =
new SaveFileDialog();

saveFileDialog.FileName = "foo.txt";

// OverwritePrompt is true by default, but I
// explicitly set it for clarity.

saveFileDialog.OverwritePrompt = true;

if (saveFileDialog.ShowDialog() !=
DialogResult.Cancel)
{
StreamWriter writer = null;
try
{
writer = new StreamWriter
(saveFileDialog.OpenFile());

writer.Write (text);
writer.Flush();
}
finally
{
writer.Close();
}
} // !Cancel
} // SaveFile
} // Sample
} // Frustration
 
Back
Top