B4J Question Removing settings & temp files with Inno Setup [UninstallDelete]

Chris2

Member
Licensed User
I'm packaging my app with B4JPackager11 & Inno Setup as described at https://www.b4x.com/android/forum/t...the-simplest-way-to-distribute-ui-apps.99835/.

My app creates a number of temporary files during use (e.g. user settings, temporary png files etc), These are written to File.DirData.

To keep things tidy, I'm trying to use the Inno Setup [UninstallDelete] section to remove the settings & temp files when the app is uninstalled (http://www.jrsoftware.org/ishelp/ - 'Setup Script Sections'>[UninstallDelete] section).
So, I have;
B4X:
[UninstallDelete]
Type: files; Name: "{userappdata}\MyApp\*.png"
Type: files; Name: "{userappdata}\MyApp\settings"
Type: dirifempty; Name: "{userappdata}\MyApp"
in my Inno Setup script.
{userappdata} is listed as a constant in the Inno Setup help docs and {userappdata}\MyApp\ seems to be equivalent to File.DirData.

BUT, I get a warning when compiling the Inno Setup script;
B4X:
Warning: The [Setup] section directive "PrivilegesRequired" is set to "admin" but per-user areas (userappdata) are used by the script.
Regardless of the version of Windows, if the installation is running in administrative install mode then you should be careful about 
making any per-user area changes: such changes may not achieve what you are intending. See the "UsedUserAreasWarning" topic in help file for more information.
Which makes perfect sense; the admin user running the uninstall won't necessarily be the user of the app, so during the uninstall {userappdata} may not point to the correct user folder.

I want the user to be able to install to the C:\Program Files\ folder, so I think the install/uninstall will have to be done with admin privileges.
My app has to be able to write the settings & temp files even if the user is non-admin, hence I'm using File.DirData (which points to the C:\Users\[user name]\AppData\Roaming\[AppName] folder). I think using File.DirTemp still uses the C:\Users\[user name]\.... folder so would have the same problem.
I have also thought of deleting the temp files each time the app is closed, but I don't want to do that with the saved user settings.

Can anyone think of a way to delete the settings & temp files when the app is uninstalled?
 

Chris2

Member
Licensed User
I may have found a workaround for this;
Inno Setup has a [UninstallRun] section which
...specifies any number of programs to execute as the first step of uninstallation
.
This can include a runasoriginaluser flag which means
the spawned process will execute with the (normally non-elevated) credentials of the user that started Setup initially (i.e., the "pre-UAC dialog" credentials).
I'm thinking that I might be able to create a second program that deletes the files and folder at File.DirData and run it through [UninstallRun].

I have not yet tested this but will report back when I have in case anyone else has a similar need.

I'd also still appreciate input from anyone who can think of a better way.
 

Chris2

Member
Licensed User
I may have found a workaround for this;
Inno Setup has a [UninstallRun] section which .
This can include a runasoriginaluser flag which means

I'm thinking that I might be able to create a second program that deletes the files and folder at File.DirData and run it through [UninstallRun].

I have not yet tested this but will report back when I have in case anyone else has a similar need.

I'd also still appreciate input from anyone who can think of a better way.
Unfortunately the workaround mentioned in post #2 won't work. I hadn't seen that the runasoriginaluser flag is available only for the [Run] section of the Inno Setup script file, and can't be used in the [UninstallRun] section.
So I'm still trying to find a workable solution.
 

Chris2

Member
Licensed User
You can try to create a non-ui app that uses the embedded JRE to run and then delete the DirData files.
For anyone who finds this thread in the future, I've come up with a couple of workable solutions to this. They are both theoretical/untested as yet and are based on the quote above; creating a non-ui app that is installed alongside the main app and run at uninstall time via the [UninstallRun] section of the Inno Setup script.

Neither is perfect or particularly neat, but I think they'll work. The important bit being that they work no matter which user account they are running under, and find & delete the File.DirData("MyApp") folder that is created at run-time by the main app which could have been running under a different user account.

Option 1:
The non-ui app searches the whole Windows Users folder for folders named "MyApp" in a ...\Users\username\AppData\Roaming\ folder, & deletes the contents & folder.

Option 2:
Each time the main app is started it stores the path of File.DirData("MyApp") in a file located in the Public users folder (..\Users\Public).
This is accessible by all users on the PC, and (I think) can always be found with;
B4X:
'find Public user folder - don't assume it's at C:\Users\Public
Dim userAppDataRoaming As String =File.DirData("")
Dim userAppData As String = File.GetFileParent(userAppDataRoaming)
Dim userPath As String = File.GetFileParent(userAppData)
Dim usersPath As String = File.GetFileParent(userPath)
Dim userPublic as string = File.Combine(usersPath, "Public")
At uninstall time the non-ui app reads the path in this file and deletes the folder & contents.
 
Top