Where am I going wrong. I want to read “textfile.txt”. If I hardcode it, it craps out.
Currently the below pops up a requestor and I select the file, reads correctly. If I force it and tell it the specific file, it fails at textinputstream.open.
Where am I going wrong?
var f As FolderItem
Var textInput As TextInputStream
Var rowFromFile As String
f = FolderItem.ShowOpenFileDialog(“text/plain”)
If f <> nil and f.exists Then
textInput = TextInputStream.Open(f)
textInput.Encoding = Encodings.UTF8
rowFromFile = textInput.ReadLine
if rowFromFile.left(6) = “dbloc=” then
gdbLocation = rowFromFile.right(len(rowfromfile) - 6)
Loop Until textInput.EndOfFile
msgbox(“File not found.”)
If you’re on macOS it may be because your app does not have permission to access the Desktop.
Part of the Apple’s security standards are that developers must request file access from the user using an open or save dialog. The only location you’re allowed access to without permission is
@Sam_Rowlands reports that on newer macOS versions,
SpecialFolder.Temporary may not always be accessible.
Use the code shared in TextInputStream and add inside the read process code to achiev your goad.
It miss this:
Var f As FolderItem = FolderItem.ShowOpenFileDialog(“text”) // as defined in File Type Sets Editor
If f <> Nil Then
If f.Exists Then
// Be aware that TextInputStream.Open could raise an exception
Var t As TextInputStream
t = TextInputStream.Open(f)
t.Encoding = Encodings.UTF8
TextArea1.Value = t.ReadAll
Catch e As IOException
MessageBox(“Error accessing file.”)
Also, avoid MsgBox; use instead (for degugging):
and read it on real time while the code executes… (if you have the reporting window part visible.
No offense, but that didn’t help any. My code works if I pop upba requestor. If I hardcode it fails at textinputstream.open. adding try and the other stuff isnt needed for my example.
It could help.
Also, I think I recall something like FolderItem isReadable / Permissions:
It can be useful in the example while you are in debug mode.
Fails to open the file as stated in original post.
What I’m trying to do is open a specific file without asking for the end-user to select the file with a requestor.
Are you getting an exception? What does it say?
Has there ever been a time where you’re reading docs and know you’ve read it correctly but no matter what you do you’re wrong? Then you realize PEBKAC?
My filename was misspelled. Sigh.