New FolderItem throws UnsupportedFormatException only on Windows

Scenario: You need to create a folder tree, but only if it doesn’t exist. The path is stored as a string like this:

Var requiredPath As String = SpecialFolder.Documents.NativePath + "/MyNewRoot/MyNewChild"

When TargetWindows is true, you convert the slashes to backslashes, and clean it up, for example:

Var pathChar As String = “/"
If TargetWindows Then pathChar = “\"
// Regardless of OS and how the path was stored, it will always come out “clean”
requiredPath = requiredPath.ReplaceAll(“/",pathChar).ReplaceAll(“\",pathChar) // Convert all path delimiters to OS standardized
requiredPath = requiredPath.ReplaceAll(pathChar+pathChar,pathChar) // Remove duplicate path chars

Then, the moment of truth comes, and you attempt to create the FolderItem:

Var fi As FolderItem = new FolderItem( requiredPath )

  • MacOS executes this line WITHOUT Exception.
  • BUT Windows throws AN “UnsupportedFormatException”
    • Apparently, Windows “new FolderItem” Is internally creating something like: SpecialFolder.Documents.Child(“MyNewRoot”).Child(“MyNewChild”)
    • That construct would obviously throw an error since the .Child(“MyNewRoot”) would be a Nil object (and therefore cannot have .Child method).
  • Is this a Xojo bug?
  • Or is there some obscure reason why the “new FolderItem” is more robust on MacOS (even though cross-platform apps can’t enjoy the robustness)?

According to the docs, the macOs is the less robust one, due to the reasons you pointed out. Trying to point to a nonexistent target at a nonexistent folder all at once. It should rise the exception on Mac too instead of let it pass this way.

Now that you found the culprit, just a write a code able to handle whatever you had in mind when pointed things to such double nonexistent place.

macOS is misbehaving. According to the docs:

"If Path cannot be resolved to a FolderItem, an UnsupportedFormatException is raised. This is notably the case when a folder does not exist within the given Path or when you do not have the correct access permissions for something in the path. Only the last component of the path is allowed not to exist."

I think that an Issue Report to fix the Mac behavior should be done.

1 Like

Good info … thanks!

1 Like