Create & Save my DB file to the apps directory with button click

I want to get the current directory my application is running from and be able to save my newly created sqlite database there.

I don’t want to use the file/Open/Save dialogues. I want to press a button, it creates a database and save sit to the programs folder.

Can any assistance be offered.

If you are on macOS and your app is in the application folder then you can’t write anything in the application folder.

Below is my code to create or open an SQLite Database in application support folder:

theDatabase = New SQLiteDatabase
dim theFolderitem as FolderItem = SpecialFolder.ApplicationData.Child(app.getMyAppName)
if theFolderitem <> Nil and theFolderitem.Exists then
  theFolderitem = theFolderitem.Child(AccountName + ".db")
  isConnectedToDB = False
end if

theDatabase.DatabaseFile = theFolderitem

'database is there, just connect
If theFolderitem.Exists Then
    isConnectedToDB = True
  catch err as DatabaseException
    isConnectedToDB = False
  end try
  'create database and table
    Dim sql As String = "CREATE TABLE ""Data""(""MessageID"" Text PRIMARY KEY, ""File"" Text NOT NULL, ""Found"" boolean, ""FileSize"" Integer)"
    isConnectedToDB = True
  catch err as DatabaseException
    isConnectedToDB = False
  end try
End If

You cant do that on Windows either.
Beatrix’s code works on both platforms.

The only time this is not the case is if you put your app on a USB drive or some other unusual place.
Dont fight it.
Do it right. :slight_smile:

Hi! I’m on windows 11. Where does it create this folder then? Also, as it is stated that saving to the application folder is not possible, is saving to a folder within the application folder possible then?

Thank you for the code, also. It is very easy to read and understand and helps me a lot.

The database is saved here:

I understand but which folder is it and what is the folders name? Would it be inside the application folder and then whatever I assigned to getMyAppName?

[edit]*** I’m not getting any file being created anywhere I can recognize.

Don’t know about Windows but on the Mac the Application Support folder is in the Library:

There are 3 databases in my folder.

On Windows, the application support folder is hidden by default.

If you are looking for your file, you need to enable viewing hidden files and folders in File Explorer.

The example code has a suggested way to get the folder name, but you can hard code it, or use your company name.

the parent folder could be:


The database will be
SpecialFolder.ApplicationData.child(“myCompany”).child(AccountName + “.db”)

ApplicationData itself may appear as AppData , ApplicationData , or similar, and may be under Users, Roaming or other locations depending upon how your machine and network is configured.
However, the reason the folder is hidden is that Microsoft want to make it hard for people to stumble across files there and delete important stuff.

If you WANT people to find your files and maybe do something with them not using your app, use SpecialFolder.Documents instead.

Ahh, ok. I definitely do not want the db file saved in AppData. The Documents folder is a lot better for my needs as it will allow the user to backup the db to a custom location of their own.

SpecialFolder.Documents is what I need.

Thank you all for your help.

description file is here:

Now, you can use SpecialFolder.ApplicationData, and provide a way to allow the user to open that folder directly.

In an application (for newbies), I provided a lot of folders (that holds backup, etc.), and have a window with buttons (the special folders icons) that allow the user to directly open the folder (s)he want (Application, db, service files, etc.).

This allowed me to not go to the client location to make simple repairs, etc. Their question were "But where is the db file ?” (for example). Now the answer is “Open the xyz MenuItem from Menu T and click in c.sqlite”.

My app does what the OP wants to do, but it only does that when it is NOT in the Applications folder. That means you can put the app and its datafolder on a stick, plug that into another machine, and run it from the stick. That was the concept.

Try this:

Var  herefi as folderitem, here as str

herefi = new FolderItem ("", FolderItem.PathModes.Native)
here   = herefi.NativePath

And where goes the Preferences files (both: the one your application creates and the ones the OS deals with) ?

In the app’s datafolder. Which, when the app is in the Applications folder, is in the user’s Documents folder.

And when - following your case- the application is on a MemoryStick ?

Then it knows it is not in the Applications folder and looks next to itself for the datafolder.

I’m confused a little. Some people say it is possible and others say it’s not. I needed it to be possible when not on a USB but from what I have learned there is a security issue with letting things save stuff to the applications folder.

If my app was say in this folder: MyApp:
Could I place my database in : MyApp\Databases


I would need to ship with the databases folder already created but if it were deleted I would have to create one when the app starts, is any of that possible?

Yes, I told you how to find where the app is located. But you need to understand that in general your app can’t write to anywhere inside the Applications folder. In general that will be prevented by the OS so don’t try it. If, however, your app is not in the Applications folder, but is elsewhere, then you probably can, as I described. You just need some extra logic, that’s all.

1 Like
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.