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")
else
isConnectedToDB = False
Return
end if
theDatabase.DatabaseFile = theFolderitem
'database is there, just connect
If theFolderitem.Exists Then
try
theDatabase.Connect
isConnectedToDB = True
catch err as DatabaseException
isConnectedToDB = False
end try
else
'create database and table
try
theDatabase.CreateDatabase
Dim sql As String = "CREATE TABLE ""Data""(""MessageID"" Text PRIMARY KEY, ""File"" Text NOT NULL, ""Found"" boolean, ""FileSize"" Integer)"
theDatabase.ExecuteSQL(sql)
isConnectedToDB = True
catch err as DatabaseException
isConnectedToDB = False
end try
End If
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.
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.
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:
SpecialFolder.ApplicationData.child(“myCompany”)
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.
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
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.