Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SaveFileDialog exception in my WPF app

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

My save file code is:

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

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

And here is the exception they are getting:

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()

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

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?

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.

like image 824
Ross Avatar asked Mar 26 '12 15:03

Ross


1 Answers

I found that using

fullPath = System.IO.Path.GetFullPath(relPath);

eliminated the problem for me. Apparently, FileDialog.ShowDialog does not like relative InitialDirectory values.

like image 125
shadow Avatar answered Sep 29 '22 06:09

shadow