我的WPF应用中的SaveFileDialog异常 [英] SaveFileDialog exception in my WPF app

查看:323
本文介绍了我的WPF应用中的SaveFileDialog异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一位客户在保存文件时遇到了我的WPF应用程序崩溃的情况.

One of my customers is experiencing a crash in my WPF application when saving a file.

我的保存文件代码是:

var saveFileDialog = new SaveFileDialog {
  InitialDirectory = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"\MyApp"),
  FileName = "MyFile", 
  OverwritePrompt = true,
  AddExtension = true
};

if (saveFileDialog.ShowDialog() == true) {
  ...
}

这是他们得到的例外:

Value does not fall within the expected range.

A System.ArgumentException occurred
   at MS.Internal.Interop.HRESULT.ThrowIfFailed(String message)
   at MS.Internal.AppModel.ShellUtil.GetShellItemForPath(String path)
   at Microsoft.Win32.FileDialog.PrepareVistaDialog(IFileDialog dialog)
   at Microsoft.Win32.FileDialog.RunVistaDialog(IntPtr hwndOwner)
   at Microsoft.Win32.FileDialog.RunDialog(IntPtr hwndOwner)
   at Microsoft.Win32.CommonDialog.ShowDialog()

(最后一行中的ShowDialog表示我在上面的代码中进行的调用.)

(Where the ShowDialog in the last line refers to the call I make in my code above.)

所以我的直觉是,对于我的客户而言,对Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)的调用返回的是SaveFileDialogInitialDirectory不一样的东西.我在网络搜索(并经过验证)中发现,当传递相对路径作为SaveFileDialog的InitialDirectory时,会发生此错误. Environment.SpecialFolder.MyDocuments是否有可能作为相对路径返回?如果不是,是否有人知道另一种可能无效的格式?可能是某个SpecialFolder.MyDocuments网络路径引起的吗?还有其他想法吗?

So my hunch is that in my customer's case, the call to Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) is returning something that the SaveFileDialog does not like as the InitialDirectory. I've found in web searches (and validated) that this error occurs when passing a relative path as the InitialDirectory of the SaveFileDialog. Is it possible that Environment.SpecialFolder.MyDocuments could be returned as a relative path? If not, does anybody know another potentially invalid format? Could a certain SpecialFolder.MyDocuments network path be the cause? Any other ideas?

我无法直接访问客户的机器,他们也不是精通技术,所以不可能100%确定发生了什么.

I don't have direct access to my customer's machine and they aren't particularly tech savvy so it's not possible to be 100% certain what is happening.

推荐答案

找到了它.

InitialDirectory = string.Concat(
    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
    @"\MyApp"
)

Environment.SpecialFolder.MyDocuments在我的客户的计算机上返回,结尾带有'\'字符,因此完整的串联路径中有一个双'\'.

Environment.SpecialFolder.MyDocuments was being returned on my customer's machine with a trailing '\' character, and so the complete concatenated path had a double '\' in it.

SaveFileDialog崩溃(我认为这是一个缺陷-它应该更合理地处理或强制无效输入).

SaveFileDialog crashes when you pass an InitialDirectory path containing a double '\' (which is a flaw in my opinion - it should more gracefully handle or coerce invalid inputs).

我现在使用Path.Combine静态方法来处理这两种变体:

I use the Path.Combine static method instead now to handle both variants:

InitialDirectory = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
    "MyApp"
)

它不再崩溃.

这篇关于我的WPF应用中的SaveFileDialog异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆