发生错误时如何关闭Win32 SaveFileDialog?

发布于 2024-11-30 02:09:31 字数 1054 浏览 0 评论 0原文

我在 Wpf 应用程序中遇到了 Microsoft.Win32.SaveFileDialog 的问题。

如果用户在 SaveFileDialog 中输入一个巨大的文件路径,超过允许的最大值(我认为是 255 个字符?),那么它开始变得不可用(详细信息在代码示例中)。

因此,作为解决方法,我想关闭对话框并让它们再次输入文件路径。然而问题是 SaveFileDialog 没有 Close() 例程或我可以看到的其他任何东西来关闭它。如何以编程方式关闭对话?

// 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);
}

编辑

感谢您的回答/评论。我刚刚在我的 Windows 7 机器上对此进行了测试,它的行为符合预期,因此这可能只是 XP 上的问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无语# 2024-12-07 02:09:31

在 Windows 7 上的 WPF 4.0 中,SaveFileDialog 显示其自己的错误对话框:

<long path?
The path is too long.
Try a shorter name.

使用“确定”按钮来关闭错误对话框。这将导致用户返回到原始的 SaveFileDialog,他们可以在其中更改其值或取消。

对于行为可能不同的早期版本,您可以使用Windows UI自动化框架,用于以编程方式单击 SaveFileDialog 上的“取消”按钮

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.

み青杉依旧 2024-12-07 02:09:31
if (dialog != null)
{
    dialog.DialogResult = DialogResult.Cancel;
}

尝试设置对话框结果以关闭对话框。

if (dialog != null)
{
    dialog.DialogResult = DialogResult.Cancel;
}

Try setting the dialog result to close the dialog.

弃爱 2024-12-07 02:09:31

向窗口发送 WM_SYSCOMMAND 消息,其中包含SC_CLOSE 的 wParam 参数。这相当于单击对话框右上角的“关闭”按钮。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文