Trying to read a resource (within app bundle put there using build step) using SpecialFolder.GetResource. The resource is there. The FolderItem instantiates with the resource.
Upon attempting to Open resource for reading using TextInputStream XOJO throws error 2 (of course, no Message or Reason provided.)
After checking, the FolderItem returned has “IsReadable=False” — is this normal and if so, how does one read a resource such as a text file then?
Note that code works perfectly when accessing this text file from a directory, including a system folder like application support.
Thanks in advance!
I would expect isWriteable to return false and isReadable to return true, as items should be readable. I store various files in the resources folder of my application that my applications can read, which include a HTML file template, that is loaded and has data injected into it. Below is the code I use to read the file from the resources folder.
Dim templateFile as folderItem = app.folderResources.child( "bookInfoTemplate.html" )
Dim tis as textinputStream = textInputStream.open( templateFile )
Dim templateContents as string = tis.readAll
This code works under macOS 10.13.6 ~ 11.0.1, with Xojo 2020r1.2. The file is added to the application using an alpha version of App Wrapper 4 and the app.folderResources function is part of my own AppKit.
Thanks Sam, I don’t see “folderResources” under the App object. That’s a property of type FolderItem that you’ve defined under your App object?
Dim textInput As TextInputStream
myFile = SpecialFolder.Resource(“data”).Child(“vad.csv”) ’ myFile defined as FolderItem elsewhere
textInput = TextInputStream.Open(myFile) <- xojo blows up here…
rawValenceData = textinput.ReadAll ’ rawValenceData defined elsewhere as string
It’s a function in a module that uses extends to operate on the app object. It is property backed so that I can change it during debugging (rather than adding all the files for each debug build).
Does the data folder exist in the resources folder?
Dim dataFolder as folderitem = specialFolder.resource( "data" )
if dataFolder = nil or dataFolder.exists = false then
break // --- Should really display an error.
myfile = dataFolder.child( "vad.csv" )
Definitively not normal.
If you put a breakpoint at the line which tries to open the file (i.e. just before you get the error 2), does your folderitem also has IsReadable to false?
I’m thinking the TextInputStream call could be the culprit, triggering some mysterious bug under the hood.
When IsReadable is false, are other properties still valid and as they were prior to the exception?
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.