SaveFileDialog exception in my WPF application

One of my clients is experiencing a crash in my WPF application while saving the file.

My save file code:

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 get:

 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 that I make in my code above.)

So my hunch is that in my client case, calling Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments) returns what SaveFileDialog doesn't like as InitialDirectory . I found in a web search (and confirmed) that this error occurs when passing a relative path as InitialDirectory to SaveFileDialog. Is it possible that Environment.SpecialFolder.MyDocuments can be returned as a relative path? If not, does anyone know of another potentially invalid format? Could there be a reason for the specific network path SpecialFolder.MyDocuments? Any other ideas?

I do not have direct access to my car for customers, and they are not particularly effective in technology, so it is impossible to be 100% sure what is happening.

+4
source share
3 answers

Found.

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

Environment.SpecialFolder.MyDocuments returned to my client machine with a trailing "\", so the full concatenated path had a double "\" in it.

SaveFileDialog crashes when passing the InitialDirectory path containing a double '\' (which, in my opinion, is a drawback - it should be more graceful to handle or force invalid inputs).

Instead, I use the static Path.Combine method to handle both options:

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

And it doesn't work anymore.

+1
source

I found that using

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

fixed the problem for me. Apparently, FileDialog.ShowDialog does not like the relative values ​​of InitialDirectory .

+6
source

For those who had the same problem:

An exception also occurs when Environment.SpecialFolder.MyDocuments points to a network drive (domain environment) and is somehow inaccessible. Then GetFullPath or Path.Combine does not help.

I decided that he caught the exception and called ShowDialog a second time after setting InitialDirectory to the system root, for example. "C: \".

0
source

Source: https://habr.com/ru/post/1403498/


All Articles