Search code examples
inno-setuppascalscript

How to reference the files in the Files section from Code/Pascal section?


In my code section I need to modify an existing configuration file (Apache httpd.conf) to include one of the files I'm installing in the Files section.

How can I reference my .conf file, so I can insert into the httpd.conf something like this:

Include "C:/Program Files (x86)/Apache Software Foundation/Apache2.4/conf/myinclude.conf"

I think I can do something like:

 ExtractFilePath( {app} ) + '\conf\myinclude.conf' 

to get the full path of the file.

However that means I have to hard code the partial path in my script code. If we later change the path then I have to change it in the files section and remember to change it in the script code too.

Is there a way to reference the file just by name and get the full installed path?

Secondary question:
What's the best place to do this kind of thing (modifying a file)?

  1. In the AfterInstall of the file I'm going to modify it for?
  2. In NextButtonClick on wpFinished?
  3. Other?

Solution

  • Use a preprocessor constant/variable.

    The best place to update the httpd.conf is CurStepChanged(ssPostInstall). The AfterInstall will do too.

    #define MyIncludeName "myinclude.conf"
    #define MyIncludeRelPath "conf\" + MyIncludeName
    
    [Files]
    Source: "{#MyIncludeName}"; DestDir: "{app}\..\{#MyIncludeRelPath}"
    
    [Code]
    
    procedure CurStepChanged(CurStep: TSetupStep);
    var
      Path: string;
    begin
      if CurStep = ssPostInstall then
      begin
        Path := ExtractFilePath(ExpandConstant('{app}')) + '{#MyIncludeRelPath}';
        ...
      end;
    end;