Inno Setup - puts user files in admin documents

2020-02-13 06:46发布

问题:

I have a few Windows 7 users who, when installing and logged in as themselves, are asked to provide the Admin password. When this happens, Inno Setup installs the program for that user but it places the sample data files in the Admin's documents folder.

What can I do to make sure Inno Setup places the sample data files in the user's Documents folder where they belong?

[Files]
Source: "C:\dev\Installer Files\Chess Openings Wizard 2016\Game Trees\*.*";   DestDir: "{code:GetDataDir}\Game Trees";   Flags: uninsneveruninstall recursesubdirs

function GetDataDir(Param: String): String;
begin
  { Return the selected DataDir }
  Result := DataDirPage.Values[0];
end;

回答1:

Your approach is not correct.

There two correct ways:

  1. If the installer installs the application for the current (unprivileged) user only, do not require Administrator privileges.

    PrivilegesRequired=lowest
    

    Then the {userappdata} constant (and similar) will correctly refer to the current user's folder.

  2. If the installer installs the application for all users, it does not make sense to put some files to folder of one specific users. All users need the files, not just the one. In this case the recommended approach is to install the files to "Common" folder, using the {commonappdata} constant (or similar). And have the application copy the files to the user folder on the first run.

    See also How to write to the user's My Documents directory with installer when the user used 'Run As Administrator'.

You can also allow the user choose between these two approaches.
See Make Inno Setup installer request privileges elevation only when needed.

For another similar questions, see

  • Inno Setup Using {localappdata} for logged in user
  • Inno Setup always installs into admin's AppData directory

Having that said, you can do, what you ask for, by executing an external copy utility (copy, xcopy, robocopy) using the ExecAsOriginalUser function (or the runasoriginaluser flag in the [Run] section).

ExecAsOriginalUser(
  'cmd.exe', '/c xcopy.exe "sourcefile" "%APPDATA%"',
  '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

For more detail on this approach, see a similar question Inno Setup Creating registry key for logged in user (not admin user).



标签: inno-setup