Opening Files on Different Operating Systems

I am coding a project on my mac. The code below works perfectly on mine but when attempting to run the program on a windows operating system, the program runs fine but for some reason is not able to find the files. I am not sure if it’s an operating system issue or the syntax of my code.

f = GetFolderItem(“”).Child(“somefile”).Child(“sometextfile.txt”)
If f <> Nil Then
If f.Exists Then
// Be aware that TextInputStream.Open could raise an exception
Var t As TextInputStream
Try
t = TextInputStream.Open(f)
t.Encoding = Encodings.UTF8
list = t.ReadAll
Catch e As IOException
MessageBox(“Error accessing file.”)
End Try
t.Close
End If
End If

It works on the Mac? As it is presented here, f is a text file that is a child of another text file. I wouldn’t expect that to work anywhere.

If I understand what you’re trying to do, you could loop and redefine f for each one at a time. I get the impression that both files are in your application folder.

yes it does work on a mac and the folder is in the application folder.

As in your example though, it’s not “in” another text file is it?

Mac ".app"s are just a container that are kind of obfuscated from the average user, unlike a windows executable that is a standalone file (that took me a long time to understand coming from a windows world—“How does one click on a folder and an application runs??” Oh well, it’s just how Apple did it).

Perhaps if you could explain a little more what it is you are trying to do. Are you trying to work this across platforms or just on Mac? Are you running compiled binaries on both or is one just the debugger, or just the IDE? IDE on both, etc… ,

Essentially, you’re going to have to be aware of where something is installed, ie users application folder, executable folder, debugging etc. Is the item installed at all where you’re testing it? Are you sure it’s there?

What’s the actual error you’re getting?

What version of Xojo? GetFolderItem is deprecated now, and you should use: https://documentation.xojo.com/api/files/folderitem.html.Constructor(path_as_String,_pathMode_as_FolderItem.PathModes,followAlias_as_Boolean%3D_true).

oh yeah its a typo the text file is in a file, I fixed the typo, sorry. I’m working on a project that works across mainly Windows and Mac. The project is running using the IDE on both operating systems. There is no error when running it on the Windows OS and both the Windows and Mac has the latest version of Xojo.

What do you get from “f” as a result on both systems if you enter:

MessageBox(f.NativePath)
1 Like

Let me try that out and update you.

The reason I ask is because debug apps and built apps will point to different locations and if you’re running from the IDE on one (either) system and then from a built app, you’re going to get different results, eg:

IDE: /Users/me/Projects/xojotesting/test1/test2.txt
Built: /Users/me/Projects/xojotesting/Builds - foldertest/OS X 64 bit/test1/test2.txt
Built*: /Applications/test1/test2.txt

  • built and added to the applications folder

If you are opening paths across different operating systems, you need to take that into consideration when fixing your path into your code.

Take a look at: SpecialFolder — Xojo documentation

Then you should look at creating filepath that is defined per platform

Well the SpecialFolder words and the printing out the F.Native was helpful but is there any way I could access the folder regardless of where it is placed on the computer. I am currently using SpecialFolder.CurrentWorkingDirectory but is there a better alternative?

So the Mac and window versions had two different types of code
Windows worked with this:
f = SpecialFolder.ApplicationData
f = GetFolderItem(f.NativePath, _
FolderItem.PathTypeShell)
While mac worked with this:
f = GetFolderItem
worked just fine
Thank you for your help

That’ll do it every time (if versions are using different paths).

Glad you sorted it out.

You don’t need both of these lines. Just the first one. The second converts it to text and back Into a folderitem (and may fail I might add because you are mixing Native and Shell paths there)

The first one alone for some reason will find the text file but does not give me the contents in the text file. So it appears empty on my screen.