How to allow installation only to a specific folde

2019-01-24 21:40发布

问题:

I would like to install my setup content only to one specific directory, so I want to have the Next button on directory selection page disabled, unless the user chooses the right folder to install to.

How can I disable the Next button on directory selection page and enable it right after user chooses a specific directory ?

回答1:

The following sample shows how to disable the Next button when you reach the SelectDir page and enable it only when you enter (or choose from the browse directory dialog) the C:\MySecretDir folder (the MySecretDir constant). The comparing is case insensitive since user can enter whatever he (or she) wants.

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
const
  MySecretDir = 'C:\MySecretDir';

procedure OnDirEditChange(Sender: TObject);
begin
  WizardForm.NextButton.Enabled := CompareText(WizardDirValue, MySecretDir) = 0;
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  if CurPageID = wpSelectDir then
    OnDirEditChange(nil);
end;

procedure InitializeWizard;
begin
  WizardForm.DirEdit.OnChange := @OnDirEditChange;
end;

Or if you want to enable the Next button only if there's a specific file MyUniqueFile.exe in the chosen directory, modify the code in OnDirEditChange event handler this way:

procedure OnDirEditChange(Sender: TObject);
begin
  WizardForm.NextButton.Enabled := FileExists(AddBackslash(WizardDirValue) +
    'MyUniqueFile.exe');
end;


回答2:

You could stop users from changing the install directory, by disabling the directory selection page. When disable the page will not be shown during the install.

[SETUP]
DisableDirPage=yes


标签: inno-setup