2

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.

Siy Williams
  • 2,396
  • 1
  • 17
  • 26
  • which version are you using? For me the behavior is different that what you describe. – Philipp Schmid Aug 15 '11 at 16:52
  • @Philipp my test harness is .Net 4 Client Profile, I'm on a Windows XP SP3 machine and the path I use is _C:\0123456789abcdefghijklmnopqrstuvwxyz\0123456789abcdefghijklmnopqrstuvwxyz\0123456789abcdefghijklmnopqrstuvwxyz\0123456789abcdefghijklmnopqrstuvwxyz\0123456789abcdefghijklmnopqrstuvwxyz\0123456789abcdefghijklmnopqrstuvwxyz\0123456789abcdefghijkl\juicyFile_ – Siy Williams Aug 15 '11 at 16:54

3 Answers3

1

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.

Philipp Schmid
  • 5,778
  • 5
  • 44
  • 66
  • I had this at first when my I tried to reproduce my testers bug, however if you create all the folders first then navigate within the SaveFileDialog to the last folder and try to save the file the error occurres – Siy Williams Aug 15 '11 at 16:59
  • I have tried to reproduce your behavior on Windows 7. The dialog.ShowDialog() call returns true, but the dialog.FileName is String.Empty. I don't have an XP box to test your scenario. – Philipp Schmid Aug 15 '11 at 17:07
  • Thanks for trying. I'll try my code on my Windows 7 box when I get back home. I've reproduced it on a colleagues machine also but it is also XP; I've also tested against .Net 4 and the error still occurs – Siy Williams Aug 15 '11 at 17:13
  • 1
    I ran the same test as Phillip with similiar results. I have Windows 7 64 bit running. Could be an XP related issue. If it is, you might be forced to write your own save file dialog instead of using the out of the box one. I never understood why WPF doesn't have it's own built controls instead of going back to the Winforms one. – Jon Raynor Aug 15 '11 at 17:15
  • @Jon I was pretty suprised when I found out WPF had no Open/Save dialogs too but in reality its such a minor bug that it probably doesn't warrant all the time in a complete re-write. Only problem is when it does happen there is no recovery as it terminates the app – Siy Williams Aug 15 '11 at 19:46
0
if (dialog != null)
{
    dialog.DialogResult = DialogResult.Cancel;
}

Try setting the dialog result to close the dialog.

Jon Raynor
  • 3,804
  • 6
  • 29
  • 43
  • [SaveFileDialog](http://msdn.microsoft.com/en-us/library/microsoft.win32.savefiledialog.aspx) doesn't have a `DialogResult` property – Siy Williams Aug 15 '11 at 16:41
  • DialogResult dresult = dialog.ShowDialog(); But if I try to put in a really long file name under .Net 4.0 WPF I get a messagebox saying that the path is too long. What condition is the unhandled exception occurring? The SaveDialog control seems to be handling the long file name issue, but there may be another case where it is not handling it correctly. Shouldn't need a try/catch here. – Jon Raynor Aug 15 '11 at 16:56
  • see my comment on Philipp Schmid's answer – Siy Williams Aug 15 '11 at 17:00
  • Check the XP SP level, according to MS should be 3. http://msdn.microsoft.com/en-us/library/microsoft.win32.savefiledialog.aspx Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2 The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements. – Jon Raynor Aug 15 '11 at 17:18
0

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • That's probably the best solution. So how does one determine the handle of the dialog's window? – Gabe Aug 15 '11 at 17:23
  • @Gabe, see http://stackoverflow.com/questions/1556182/finding-the-handle-to-a-wpf-window – Mark Ransom Aug 15 '11 at 17:31
  • Sorry, but I don't follow. The referenced question has to do with WPF windows, not dialogs. The problem here is getting the handle of the dialog, not the owner window. – Gabe Aug 15 '11 at 17:42
  • @Gabe, sorry - I was hoping that the Handle property would be universal for any window object. I don't use C# or WPF so I'm out of my element on that part of your question. – Mark Ransom Aug 15 '11 at 17:49
  • OK, but do you know how to get the hwnd of a file dialog in *any* framework/language combination? If so, it can just be adapted to WPF. – Gabe Aug 15 '11 at 17:58
  • @Gabe: Wow that turns out to be a lot harder than I thought. I can't find any way to get the handle of a SaveFileDialog or CommonDialog. It might be worth opening up a new question. – Mark Ransom Aug 15 '11 at 18:55
  • This is looking the most likely answer but we just need the final piece :) – Siy Williams Aug 15 '11 at 19:47