In a lot of cases you want to refer to where your project is located for easy testing and then where your eventual product is installed such as where the exe resides on Windows. Is there a way to do this like in Java with System.getProperty(“user.dir”)? Having trouble finding an answer in the docs.
See app.ExecutableFile.Parent or shell out to “cd”.
Build scripts have ProjectShellPath and CurrentBuildLocation.
And for getting the parent folder of your app, there’s also this:
var f As new FolderItem(“”)
I hate to be ‘that guy’, but most times someone asks this, it is because they want to have some support file, database, or document ‘next to the app’
Don’t bother trying
Put your documents in specialfolder.applicationdata , in a folder named for your business or app
If you try to put documents next to the application, OSX and Windows will not let your users write to it, and databases will not open.
If that wasn’t the plan, my apologies.
No this is mostly just to help me be lazier with testing how Xojo works (I’m new to Xojo but not programming). When I’m heading towards release I’ll probably use the documents folder so the user has easy access to these files.
I appreciate the advice though!
var f As new FolderItem(“”)
This did not work for me, said it couldn’t find the file until I have it as a full literal path
That shortcut gets you the folder that the executable is in. You would then need to traverse from there using .parent or .child.
I use code like this to locate a .INI file when my app starts:
Dim Pref As FolderItem
#If DebugBuild Then
Pref = New FolderItem("C:\Users\Eric\Documents\myappfolder")
#Else
Pref = SpecialFolder.CurrentWorkingDirectory
#EndIf
This is from a Web app so I can control what’s in the folders on the server. But for Desktop apps, see comment from @Jeff_Tullin above.
Better:
Dim Pref As FolderItem
dim prefparent as folderitem = specialfolder.applicationdata.child("myapp")
if prefparent = nil then
//invalid path.. lots to unpick
else
if not prefparent.exists then
prefparent.createasfolder
end if
pref= preparent.child("theINIFile.ini")
if not prefs.exists then
//create a file from scratch
end if
//now test/use etc
end if
Works in debug or release - both use the SAME location.
Works in Windows or Mac or Linux
Hope you didn’t just copy/paste that, and thus get curly quotes instead of the correct ones.
Var f As new FolderItem ("", FolderItem.PathModes.Native)
This definitely works, I use it in my app.
Nope, I am on Linux and this just seems to default to my home folder and not the dir with the project binary (a few of the different answers did this, maybe a Linux bug?). The answer I marked gets me the closest.