Error Saving PNG Image File

I do not understand this error that is in my program. Basically the code is like this.

DocFile.ProjectFolder.CreateAsFolder

if DocFile.SplashScreen <> nil then
dim f as FolderItem = DocFile.ProjectFolder.Child(“SplashScreen.png”)
DocFile.SplashScreen.Save(f, FolderItem.SaveAsPNG)
end if

What happens is the variable f gets set to nil. When the debugger pauses the application, I check the directory of DocFile.ProjectFolder and it is not nil and even when I check the directory above that folderitem is valid. But DocFile.ProjectFolder is not a directory that exists even after using ProjectFolder.CreateAsFolder.

Not far above this code is some code to delete the entire folder before creating one from scratch. So it seems this CreateAsFolder method doesn’t always operate for some reason. Possibly due to its folder directory being open? How would I prevent this from crashing my application and what would be appropriate to get this to work. The code seems to work but only if there is folders open.

All my application does currently is checks for if there is an error and then gives the user a message to resave.

Thanks

DocFile.SplashScreen.Save(f, Picture.SaveAsPNG)

Have you confirmed that ProjectFolder is, in fact, deleted by your code? Have you removed all files recursively, including hidden ones, before deleting the folder itself?

Above that code is this check

’ dim FolderDeleteErrorCode as int32 = DocFile.ProjectFolder.DeleteEntireFolder
if FolderDeleteErrorCode = 0 then’

This is the method for recursion and deleting the folder prior to creating it.

Public Function DeleteEntireFolder(extends theFolder as FolderItem, continueIfErrors as Boolean = false) as Integer
// Returns an error code if it fails, or zero if the folder was deleted successfully

dim returnCode, lastErr, itemCount as integer
dim files(), dirs() as FolderItem

if theFolder = nil or not theFolder.Exists() then
return 0
end if

// Collect the folder‘s contents first.
// This is faster than collecting them in reverse order and deleting them right away!
itemCount = theFolder.Count
for i as integer = 1 to itemCount
dim f as FolderItem
f = theFolder.TrueItem( i )
if f <> nil then
if f.Directory then
dirs.Append f
else
files.Append f
end if
end if
next

// Now delete the files
for each f as FolderItem in files
f.Delete
lastErr = f.LastErrorCode // Check if an error occurred
if lastErr <> 0 then
if continueIfErrors then
if returnCode = 0 then returnCode = lastErr
else
// Return the error code if any. This will cancel the deletion.
return lastErr
end if
end if
next

redim files(-1) // free the memory used by the files array before we enter recursion

// Now delete the directories
for each f as FolderItem in dirs
lastErr = f.DeleteEntireFolder( continueIfErrors )
if lastErr <> 0 then
if continueIfErrors then
if returnCode = 0 then returnCode = lastErr
else
// Return the error code if any. This will cancel the deletion.
return lastErr
end if
end if
next

if returnCode = 0 then
// We‘re done without error, so the folder should be empty and we can delete it.
theFolder.Delete
returnCode = theFolder.LastErrorCode
end if

return returnCode
End Function

@Oliver Scott-Brown it would help us if you wrap your code with [code] & [/code]. It makes it much easier to read.

why not a shell?

shell.execute ("rm -rf " + theFolder.ShellPath)

Does this work cross platform though?

should work in OSX and Linux, on Windows use rmdir
(maybe "rmdir /s /q " + theFolder.ShellPath)

[quote=311323:@Axel Schneider]should work in OSX and Linux, on Windows use rmdir
(maybe "rmdir /s /q " + theFolder.ShellPath)[/quote]
Thanks. I have Windows.

[quote=311323:@Axel Schneider]should work in OSX and Linux, on Windows use rmdir
(maybe "rmdir /s /q " + theFolder.ShellPath)[/quote]
This does not seem to work on Windows. But thanks anyway.