我从WPF应用程序获取报告,该应用程序部署在字段中,在尝试显示“打开文件”对话框时,会引发以下ArgumentException。
Exception Message: Value does not fall within the expected range.
Method Information: MS.Internal.AppModel.IShellItem2 GetShellItemForPath(System.String)
Exception Source: PresentationFramework
Stack Trace
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(Window owner)
...
问题是到目前为止我还没有能够在我的开发环境中复制这个问题,但是我收到了一些来自该领域的报告,说明发生了这种异常。
有没有人见过这个?最重要的是你知道其原因和/或解决方法,而不仅仅是简单地试一下它并指示用户再试一次他们试图做的事情吗?
在回复评论时,这是打开对话框的代码(不,这不是检查返回类型的问题)。从ShowDialog中抛出异常(请参阅堆栈跟踪):
Nullable<bool> result = null;
var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".txt";
dlg.Filter = "Text Files (.txt)|*.txt|All Files|*.*";
dlg.Title = "Open File";
dlg.Multiselect = false;
dlg.InitialDirectory = GetFolderFromConfig("folders.templates");
result = dlg.ShowDialog(Window.GetWindow(this));
if (result == true)
{
// Invokes another method here..
}
答案 0 :(得分:4)
非特殊目录(例如映射的网络驱动器)也会发生此问题。在我的例子中,我们的%HOME%
环境变量指向映射的网络驱动器(Z :)。因此,以下代码生成相同的异常:
Nullable<bool> result = null;
var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".txt";
dlg.Filter = "Text Files (.txt)|*.txt|All Files|*.*";
dlg.Title = "Open File";
dlg.Multiselect = false;
dlg.InitialDirectory = Environment.GetEnvironmentVariable("Home")+@"\.ssh"; // boom
result = dlg.ShowDialog(Window.GetWindow(this));
解决方案:
var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".txt";
dlg.Filter = "Text Files (.txt)|*.txt|All Files|*.*";
dlg.Title = "Open File";
dlg.Multiselect = false;
dlg.InitialDirectory = System.IO.Path.GetFullPath(Environment.GetEnvironmentVariable("Home")+@"\.ssh"); // no boom
result = dlg.ShowDialog(Window.GetWindow(this));
答案 1 :(得分:3)
这应该是@Hans Passant,因为他指出了我正确的方向。
事实证明,一旦我弄清楚问题究竟是什么,在我的开发计算机上复制(和修复)问题就很简单了。事实证明,问题确实是将InitialDirectory属性设置为某个奇数值。在我的情况下,我能够通过将InitialDirectory设置为“\”;
来复制该问题以下是解决此问题的修改后的代码:
try
{
result = dlg.ShowDialog(Window.GetWindow(this));
}
catch{
dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
result = dlg.ShowDialog(Window.GetWindow(this));
}
答案 2 :(得分:0)
///<summary>
/// Tries to handle inability of user to access file dialog folder
/// by using other folder. Shows meaningful message to
/// user for action on failure.
///</summary>
private bool? ShowDialogSafe()
{
try
{
return dlg.ShowDialog(Window.GetWindow(this));
}
// reacts on rare case of bad default folder, filter only folder related excepions
catch (Exception handledEx) when (IsInitialDirectoryAccessError(handledEx))
{
var original = dlg.InitialDirectory;
var possible = Environment.GetFolderPath(Environment.SpecialFolder.Personal);// hope user has access to his own, may try Desktop
try
{
dlg.InitialDirectory = possible;
return FileDialog.ShowDialog(Window.GetWindow(this));
}
catch (Exception ex) when (IsInitialDirectoryAccessError(ex))
{
var message = string.Format("Failed to access directories '{0}' and '{1}'. Please contact system administrator.", original, possible);
throw new IOException(message, ex);
}
}
}
/// <summary>
/// see GetShellItemForPath(string path) http://referencesource.microsoft.com/#PresentationFramework/src/Framework/MS/Internal/AppModel/ShellProvider.cs,f0a8bc5f6e7b1503,references
/// System.IO.FileNotFoundException: 'The network name cannot be found. (Exception from HRESULT: 0x80070043)' - no such share exsits
/// "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"} System.UnauthorizedAccessException - folder is forbidden
/// System.ArgumentException: 'Value does not fall within the expected range.' - badly formatted path
/// </summary>
/// <param name="handledEx"></param>
/// <returns></returns>
private static bool IsInitialDirectoryAccessError(Exception handledEx)
=> handledEx is IOException || handledEx is UnauthorizedAccessException || handledEx is ArgumentException;