SaveFileDialog does not handle longpath format

Duncan 1 Reputation point
2020-11-11T15:06:30.77+00:00

Targeting core 3.1 on windows 10

Setting a Microsoft.Win32.SaveFileDialog.InitialDirectory = "\?\C:\" results in the error:

System.ArgumentException: Value does not fall within the expected range.
at at MS.Internal.Interop.HRESULT.ThrowIfFailed()
at at MS.Internal.AppModel.ShellUtil.GetShellItemForPath(String path)
at at Microsoft.Win32.FileDialog.PrepareVistaDialog(IFileDialog dialog)
at at Microsoft.Win32.FileDialog.RunVistaDialog(IntPtr hwndOwner)
at at Microsoft.Win32.FileDialog.RunDialog(IntPtr hwndOwner)
at at Microsoft.Win32.CommonDialog.ShowDialog()

This is a legitimate path according to the documentation https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#short-vs-long-names

Pasting it in explorer works, Directory.Exists("\?\C:\") returns true.

saveFileDialog.CheckPathExists returns false after this is set.

I understand if this is a compatibility issue and it cant work because this is a win32 library, but as far as I understand there is no new version of SaveFileDialog that can be substituted to work around this in WPF.

Is there some trick I can use to deal with this? I could manually parse the path and remove the \?\ prefix but then if the path really is long it would (probably?) still crash.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,755 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.