3

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.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Daisetsu
  • 4,846
  • 11
  • 50
  • 70

3 Answers3

9

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.

Rob McDonell
  • 1,309
  • 9
  • 15
3

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;
Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
2

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.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • I totally understand and that's why I don't delete the primary folder which I install to. On the other hand I move a lot of automatically created and temporary files to a directory I create in the %appdata% directory. More often than not if something goes wrong it's very difficult to explain to the user that they need to find and delete that directory. If they re-install they want a fresh install. This is also why I'm prompting the user if they want to delete the settings directory. – Daisetsu Apr 11 '11 at 23:47
  • Sorry; still think this is a terrible idea. I deal with users all the time who don't understand folders properly, and will save something from Word or Excel in the wrong folder and can't find it. Some of these are important files that end up in the totally wrong place. (I had a user who told me all her Excel files disappeared, and was in a panic. Turned out she was running Word and the filter for .docx files didn't show the .xlsx files in the folder.) You don't know that everything in that folder belongs to your app, unless you're checking for specific names and content. – Ken White Apr 11 '11 at 23:56