Inno setup search for existing file

2019-07-22 13:50发布

问题:

How can I search for an existing exe file and then use that directory for my installer ?

If the exe file is not found I would like the user to browse for the path. In case the exe file is installed somewhere else.

Senario 1 (most common cases):

Default dir is c:\test\My program

This should be shown as the path on the "Select Destination Location" page When the user press Next, there should be a check. To make sure that the default dir exist (c:\test\My program) If it exist, the user should just continue to the Ready to Install page.

Senario 2 (very seldom cases):

Default dir is c:\test\My program

This should be shown as the path on the "Select Destination Location" page When the user press Next, there should be a check. To make sure that the default dir exist (c:\test\My program) If it does not exist, the user should be prompt for the path to "My program". The user should afterwards continue to the Ready to Install page. I then just trust that the user selects the correct path

How can I do this in InnoSetup ?

回答1:

I would make a file input page and let user choose the Picture.exe binary location manually, when it won't be found on expected location.

You can follow the commented version of this code:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output

[Files]
Source: "CurrentBinary.exe"; DestDir: "{app}"
Source: "PictureExtension.dll"; DestDir: "{code:GetDirPath}"

[Code]
var
  FilePage: TInputFileWizardPage;

function GetDirPath(const Value: string): string;
begin
  Result := '';
  if FileExists(FilePage.Values[0]) then
    Result := ExtractFilePath(FilePage.Values[0]);
end;         

procedure InitializeWizard;
var
  FilePath: string;
begin
  FilePage := CreateInputFilePage(wpSelectDir, 'Select Picture.exe location', 
    'Where is Picture.exe installed ?', 'Select where Picture.exe is located, ' +
    'then click Next.');
  FilePage.Add('Location of Picture.exe:', 'Picture project executable|Picture.exe', 
    '.exe');

  FilePage.Edits[0].ReadOnly := True;
  FilePage.Edits[0].Color := clBtnFace;

  FilePath := ExpandConstant('{pf}\Picture\Picture.exe');
  if FileExists(FilePath) then
    FilePage.Values[0] := FilePath;
end;


回答2:

I do a similar thing with my installer already. The first thing I do is need to read the registry value from the program, and if that registry value is absent then I select that program's default directory. For example:

DefaultDirName={reg:HKLM\Software\Activision\Battlezone II,STInstallDir|reg:HKLM\Software\Activision\Battlezone II,Install121Dir|{pf32}\Battlezone II}

Now, the user runs the installer, and it has to check if the program is in the right folder. It does so by checking that the program's executable file already exists. I use this piece of code to do so.

{ Below code warns end user if he tries to install into a folder that does not contain bzone.exe. Useful if user tries to install into addon or any non-BZ2 folder. }
function NextButtonClick(CurPageID: Integer): Boolean;
begin
  Log('NextButtonClick(' + IntToStr(CurPageID) + ') called');
  case CurPageID of
    wpSelectDir:
    if not FileExists(ExpandConstant('{app}\bzone.exe')) then begin
      MsgBox('Setup has detected that that this is not the main program folder of a Battlezone II install, and the created shortcuts to launch {#MyAppName} will not work.' #13#13 'You should probably go back and browse for a valid Battlezone II folder (and not any subfolders like addon).', mbError, MB_OK);
      end;
    wpReady:
  end;
  Result := True;
end;

The above code simply checks that the target executable exists and warns the user if it does not, giving him the chance to go back and change directories but also to go ahead with the install anyways.

Also, as you appear to be installing a patch or addon to an existing program, I recommend you set

DirExistsWarning=no
AppendDefaultDirName=false

And optionally to prevent an unnecessary screen if you are not creating start menu entries

DisableProgramGroupPage=yes


标签: inno-setup