File path is correct but does not exist

I have been trying to access the contents of a text file but Xojo does not recognize that it exists even though the file path is correct. What am I doing wrong?
I’ve tried these two ways:
1)

f = SpecialFolder.Applications.Child(“words.txt”)
f = GetFolderItem(f.NativePath,FolderItem.PathTypeNative)

f = GetFolderItem(“words.txt”)

As always:

  • Which version of Xojo?
  • macOS or Windows or Linux?
  • Which version of the OS?
  • What is “doesn’t recognise”?

f = GetFolderItem(f.NativePath,FolderItem.PathTypeNative)

doesn’t make any sense at all.

yeah sure.
Which version of Xojo?
Version 2020 Release 1
macOS or Windows or Linux?
MacOs
Which version of the OS?
MacOs HighSierra
What is “doesn’t recognise”?
Sorry for the misunderstanding, what I meant was the file path is correct but the f.Exists equals false even when the text file actually exists.

f = GetFolderItem(f.NativePath,FolderItem.PathTypeNative)
Yeah I am still trying to understand how file access works in Xojo.

Where is the file?

If there’s a file in my documents folder called “words.txt” I’d use similar code to below, for reading the contents.

Dim f as folderItem = specialFolder.documents.child( "words.txt" )

Try
  Dim tis as textinputStream = textinputStream.open( f )
  Dim myText as string       = tis.readall
  tis.close
  
Catch err as IOException
  MsgBox "There was an error that prevented the application from reading the file """ + f.displayName + """." + endOfLine + endOfLine + err.message
  
End Try

Although I wouldn’t recommend expecting to be able to read a file in the User’s documents folder, so once you’re happy that you can do something like the above, I’d suggest you investigate how to present a OpenFileDialog and ask the user where the file is.

Edit: Fixed words in wrong order, which still happens even at this age.

I’ll try out the approach you suggested. If it doesn’t work, I’ll use the second approach of presenting an OpenFileDialog to the user instead.
Thanks

Sounds like the classic ‘assuming it is OK to use a file NEXT TO THE APP’

There are two problems with this:
1/ ‘Next to the app’ is a different place for debug and release applications
2/ You are not allowed (by the OS) to use files in the application folder.

Put your file into
Specialfolder.Applicationdata.child(“words.txt”)
or Specialfolder.Applicationdata.child(“MyApp”).child(“words.txt”)
or Specialfolder.documents

If the content of words.txt never changes, drag it into your app in the IDE
It will then be available to your code as a string variable called words

2 Likes

Don’t use files next to your app.
Check if your app is translocated.

Dragging the text file into the IDE worked wonders. Thank you!!