In testing our internal tools for handling long pathnames (>MAX_PATH), I've come across the following issue with
System.Windows.Forms.SaveFileDialog
(in .Net 4.8 on Windows 10)
Exception Text
Message: The given path's format is not supported.
Exception Type: System.NotSupportedException
Source: mscorlib
StrackTrace:
at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath)
at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath)
at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String path)
at System.Windows.Forms.IntSecurity.DemandFileIO(FileIOPermissionAccess access, String fileName)
at System.Windows.Forms.FileDialog.get_FileName()
at .FileSaveAs
To repeat this, you navigate into a directory which is deeper than MAX_PATH, and click ok to save your file there.
You get the above.
That's a little disappointing considering the amount of effort to manage \\?\ we're having to do.
We're using .Net 4.8, where we've replaced System.IO.File.Create / Open / WriteAllText etc to use a NativeMethods.CreateFile from kernel32.dll to support long pathname versions of these functions. (ref. the ms LongPathnames articles and sample code)
Can we fix this SaveFileDialog issue?
Is it fixed in .Net Core out of interest? Not that we can migrate to that quite yet.
Note: You get a slightly different call stack if saveDlg.OverwritePrompt = true, but it boils down to the same issue with CannonicalisePath.
Kind Regards.