How to close Win32 SaveFileDialog when an error has occurred?
I've run into an issue with the Microsoft.Win32.SaveFileDialog
in our Wpf application.
If the user enters an enormous file path, above the allowed maximum (I think its 255 chars?), within the SaveFileDialog
then it starts to become unusable (details of this are in the code example).
So as a work-around I want to close the dialogue and make them enter the file path again. However the problem is that the SaveFileDialog
does not have a Close()
routine or anything else that I can see to close it. How can I close the dialogue programmatically?
// error only seems to occur if a filter is specified.
var dialog = new Microsoft.Win32.SaveFileDialog
{
Filter = "My juicy file (*.mjf) | *.mjf"
};
try
{
dialog.ShowDialog();
}
catch (System.IO.PathTooLongException error) // handle
{
/*
* if I handle this exception (by displaying a message to the user)
* the dialog will remain open, but if the user attempts to use it
* and enter another filename a mixture of the following exceptions
* are raised:
*
* AccessViolationException
* FatalExecutionEngineError
* ExecutionEngineException
*/
MessageBox.Show(error.Message);
}
EDIT
Thanks for you answers/comments. I've just tested this on my Windows 7 box and it behaves as expected so this maybe an issue only on 开发者_开发技巧XP.
In WPF 4.0 on Windows 7 the SaveFileDialog is showing its own error dialog:
<long path?
The path is too long.
Try a shorter name.
with an OK button to dismiss the error dialog. That leads the user back to the original SaveFileDialog where they can change their value or Cancel.
For earlier versions where the behavior might be different, you can use the Windows UI Automation framework to programmatically click the 'Cancel' button on the SaveFileDialog.
if (dialog != null)
{
dialog.DialogResult = DialogResult.Cancel;
}
Try setting the dialog result to close the dialog.
Send the window a WM_SYSCOMMAND message with a wParam parameter of SC_CLOSE. This is the equivalent of clicking on the Close button in the upper-right corner of the dialog.
精彩评论