directoryinno-setup

In Inno Setup choose a directory to install files from a pre-defined set


In this situation, I need to install a file to specific directory, but in different computer it might be in different folder so I need to check which on is correct.

For example, I have a file and it needs to install in A folder or B folder or C folder, depends on the computer has A or B or C. So I need to check them first, if the computer has B, then install the file in the B folder, etc.

I know I can use check after file's DestDir, if the directory doesn't exist then it won't install anything, but what I need is install that file to other directory.

Thanks in advance.


Solution

  • In the InitializeSetup event function, check for existence of your pre-defined set of directories and remember the one you find. Then set the default installation path to the found one using a scripted constant in the DefaultDirName directive.

    You will possibly also want to set the DisableDirPage=yes and the UsePreviousAppDir=no.

    [Setup]
    DefaultDirName={code:GetDirName}
    DisableDirPage=yes
    UsePreviousAppDir=no
    
    [Files]
    Source: "MyProg.exe"; DestDir: "{app}"
    Source: "MyProg.chm"; DestDir: "{app}"
    
    [Code]
    
    var
      DirName: string;
    
    function TryPath(Path: string): Boolean;
    begin
      Result := DirExists(Path);
      if Result then
      begin
        Log(Format('Path %s exists', [Path]))
        DirName := Path;
      end
        else
      begin
        Log(Format('Path %s does not', [Path]))
      end;
    end;
    
    function GetDirName(Param: string): string;
    begin
      Result := DirName;
    end;
    
    function InitializeSetup(): Boolean;
    begin
      Result :=
        TryPath('C:\path1') or
        TryPath('C:\path2') or
        TryPath('C:\path3');
    
      if Result then
      begin
        Log(Format('Destination %s selected', [DirName]))
      end
        else
      begin
        MsgBox('No destination found, aborting installation', mbError, MB_OK);
      end;
    end;
    

    Instead of using DefaultDirName={code:GetDirName}, you can also use DestDir: "{code:GetDirName}" in the respective entries of the [Files] section, if appropriate.