Search code examples
inno-setuppascalscript

How to force Inno Setup to set the installation folder dynamically with info from custom wizard page?


In Inno Setup I added a custom wizard page where the user can enter a suffix code that shall be added dynamically to the default directory.

Standard DefaultDirName is c:\MyApp. When the user adds a suffix 01 in an extra custom wizard page the DefaultDirName should change to c:\MyApp01.

How can this be done? Obviously I cannot use code in the [Setup] section because the code is evaluated prior to any wizard page.


Solution

  • When leaving your "suffix" page, append the suffix to the installation path.

    Additionally you have to deal with:

    • user going back to the suffix page and changing the suffix
    • re-installations (upgrades) - my solution just does not allow changing suffix for re-installations (relying on the default Inno Setup behavior, where user has a little chance to change installation path).
    #define AppId "your-app-id"
    #define SetupReg "Software\Microsoft\Windows\CurrentVersion\Uninstall\" + AppId + "_is1"
    #define SetupAppPathReg "Inno Setup: App Path"
    
    [Setup]
    AppId={#AppId}
    
    [Code]
    
    function IsUpgrade: Boolean;
    var S: string;
    begin
      Result :=
        RegQueryStringValue(HKLM, '{#SetupReg}', '{#SetupAppPathReg}', S) or
        RegQueryStringValue(HKCU, '{#SetupReg}', '{#SetupAppPathReg}', S);
    end;
    
    var
      SuffixPage: TInputQueryWizardPage;
    
    procedure InitializeWizard();
    begin
      if not IsUpgrade then
      begin
        SuffixPage := CreateInputQueryPage(wpWelcome, 'Select suffix', '', '');
        SuffixPage.Add('Suffix', False);
      end;
    end;
    
    function NextButtonClick(CurPageID: Integer): Boolean;
    begin
      // Add suffix to path, when leaving "suffix" page
      if (SuffixPage <> nil) and (CurPageID = SuffixPage.ID) then
      begin
        WizardForm.DirEdit.Text := WizardForm.DirEdit.Text + SuffixPage.Values[0];
      end;
      Result := True;
    end;
    
    function BackButtonClick(CurPageID: Integer): Boolean;
    var
      Suffix: string;
      P: Integer;
    begin
      // When going back from "select dir" page
      if (CurPageID = wpSelectDir) and (SuffixPage <> nil) then
      begin
        Suffix := SuffixPage.Values[0];
        P := Length(WizardForm.DirEdit.Text) - Length(Suffix) + 1;
        // ... and the path still contains the suffix
        // [was not edited out by the user] ...
        if Copy(WizardForm.DirEdit.Text, P, Length(Suffix)) = Suffix then
        begin
          // ... remove it from the path
          WizardForm.DirEdit.Text := Copy(WizardForm.DirEdit.Text, 1, P - 1);
        end
          else
        // if the suffix was edited out by the user, clear suffix box
        begin
          SuffixPage.Values[0] := '';
        end;
      end;
      Result := True;
    end;
    

    enter image description here

    enter image description here