I store all my program's settings in the appdata directory %appdata%/MyProgram. When there is a problem and the user has to reinstall, I want to ask whether to delete the data in that directory. I am using Inno Setup and added a custom page to prompt the user:
if DirExists(ExpandConstant('{userappdata}\MyProgram')) then
begin
appdataPage := CreateInputOptionPage(wpSelectTasks,
'Stored Settings', 'Would you like to remove settings before re-install?',
'Please specify if you would like to remove the current settings before re-installing MyProgram.', True, False);
appdataPage.Add('Remove current settings (Recommended if MyProgram is having problems).');
appdataPage.Add('Keep all my settings and just reinstall.');
//appdataPage.Add();
appdataPage.Values[0] := true;
end;
The installer doesn't put the data there, but the program generates it, thus the uninstaller won't delete it automatically. I also can't count on the uninstaller being run; a lot of users just run the installer again.
How can I handle the selection from this dialog so that if they click 'remove before re-install' I delete the folder %appdata%/MyProgram? I don't want to always delete it, but give them the option.
There could be other ways but the below should do it:
[Dirs]
Name: {userappdata}\MyProgram; BeforeInstall: BeforeInstallAppData;
...
[Code]
var
appDataDir: string;
appDataPage: TInputOptionWizardPage;
procedure InitializeWizard;
begin
appdataDir := AddBackslash(ExpandConstant('{userappdata}\MyProgram'));
if DirExists(appDataDir) then
begin
appdataPage := CreateInputOptionPage(wpSelectTasks,
'Stored Settings', 'Would you like to remove settings before re-install?',
'Please specify if you would like to remove the current settings before re-installing MyProgram.', True, False);
appdataPage.Add('Remove current settings (Recommended if MyProgram is having problems).');
appdataPage.Add('Keep all my settings and just reinstall.');
appdataPage.Values[0] := true;
end;
end;
procedure BeforeInstallAppData;
begin
if DirExists(appDataDir) and appDataPage.Values[0] then
DelTree(appDataDir, True, True, True);
end;
You don't need a custom page to do that. Just set up an optional task something like this:
[Tasks]
Name: deletefiles; Description: 'Remove any existing settings'; Flags: unchecked
Then delete those files in both the install and the uninstall section
[InstallDelete]
Type: files; Name: {userappdata}\MyProgram\*.whatever; Tasks: deletefiles
[UninstallDelete]
Type: files; Name: {userappdata}\MyProgram\*.whatever;
The files can be optionally deleted during a reinstall, but always deleted during uninstall.
Be careful how you specify the wildcard however, to make sure only the right kinds of temporary files are discarded.
You don't. :) Well, you shouldn't, anyway, and that's why uninstallers make it difficult.
The reason uninstallers don't delete it is because it's a very bad idea. What if the user made a mistake, and your uninstaller deleted tons of user-input data (like financial info if you wrote an accounting app)? What if they had unintentionally put data in the folder not related to your app? Uninstallers don't delete anything they didn't install for a reason.
The better way to handle this is the same way every other uninstaller works. Tell the user you couldn't delete the folder because it's not empty. They can then delete it (or move it elsewhere, or undo the uninstall they didn't intend to run) without any risk of accidental data loss.