gpt4 book ai didi

c# - 如何在发生错误时关闭 Win32 SaveFileDialog?

转载 作者:太空宇宙 更新时间:2023-11-03 14:13:37 25 4
gpt4 key购买 nike

我在 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 上存在问题。

最佳答案

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

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

使用 OK 按钮关闭错误对话框。这会将用户带回原来的 SaveFileDialog,他们可以在其中更改值或取消。

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

关于c# - 如何在发生错误时关闭 Win32 SaveFileDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7067663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com