installationinno-setupprivileges

Install files in different folder if the privileges is not administrator in Inno Setup


I want to use script like below

function InitializeSetup(): Boolean;
begin
  Result := not IsAdminLoggedOn;
  if Result then
  begin

to check if the user is administrator or not.

But how can I do if user is administrator, then install A.txt in C:\program files\ABC, otherwise in D:\TEST?

Can I write something to connect to [Files]?

Because I also want to use check path when installing files, if I can combine [Code] and [Files] it might be easier for me.

Excuse for my lack of knowledge, and thanks in advance.

I've tried to use this,

[Files]
Source: "..\ABC\CDE.txt"; DestDir: "{code:GetDirName}\IJK"; \
    Check: DirExists(ExpandConstant('C:\Program Files\FGH'))

But I don't know how to write the code, if I want to install more files in more path.

This one doesn't work:

function GetDirName(Param: string): string;
begin
  if IsAdminLoggedOn And DirExists(ExpandConstant('C:\ABC')) then
  begin
    Result := ExpandConstant('C:\ABC\My Program')
  end
    else
  begin
    if DirExists(ExpandConstant('C:\DEF')) then
    begin
      Result := ExpandConstant('C:\DEF\My Other Program');
    end
      else
    begin
      MsgBox('No destination found, aborting installation', mbError, MB_OK);
    end;
  end;
end;

Solution

  • Just implement the GetDirName scripted constant to return a different path for privileged and un-privileged installation.

    [Files]
    Source: "..\ABC\CDE.txt"; DestDir: "{code:GetDirName}"
    
    [Code]
    
    function GetDirName(Param: string): string;
    begin
      if IsAdminLoggedOn then
        Result := ExpandConstant('{pf}\My Program')
      else
        Result := ExpandConstant('{localappdata}\My Program');
    end;
    

    Though for this specific case, you might consider using {autopf} constant instead.

    [Files]
    Source: "..\ABC\CDE.txt"; DestDir: "{autopf}\My Program"
    

    If you want to use different folders for different files, just implement more functions like GetDirName. Though if the difference is only about subfolders, you can of course use one scripted constant function to resolve the common root folder and append the subfolder in the DestDir parameter.


    If you want to change an overall installation target, use the GetDirName scripted constant in the DefaultDirName directive:

    [Setup]
    DefaultDirName={code:GetDirName}
    
    [Files]
    Source: "..\ABC\CDE.txt"; DestDir: "{app}"
    
    [Code]
    // The same as above
    

    For a more complex example, see Make Inno Setup installer request privileges elevation only when needed.