Install files to original user's My Docs folder via Inno Setup on Windows Vista/7

六眼飞鱼酱① 提交于 2019-12-06 12:43:29

问题


In the [Run] section of an Inno Setup script, there's a flag runasoriginaluser that allows the script to run a process as the original user:

the spawned process will execute with the (normally non-elevated) credentials of the user that started Setup initially (i.e., the "pre-UAC dialog" credentials).

Is there an equivalent flag or workaround for the {userdocs} shell folder constant?

This is apparently a known limitation within Inno Setup (and other installers, generally), but I'm hoping someone knows a workaround.

Excerpt from the Inno Setup help file:

The "user" constants refer to the profile of the user running Setup. This user is often not the same as the currently logged-in user, so use the "user" constants with caution.


回答1:


The workaround I came up with was using an external script to perform the data copy and calling the script using the ExecAsOriginalUser function in the wpReady page of the NextButtonClick event function.

I'll provide more details if anyone is interested.




回答2:


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, by setting PrivilegesRequired to lowest:

    [Setup]
    PrivilegesRequired=lowest
    

    Then the "user" constants 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, as you have found yourself, by execute 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).

Though, if the installer was started elevated straight away (as opposite to elevating itself), the above won't work. And it cannot work in this scenario anyway. See How to write to the user's My Documents directory with installer when the user used 'Run As Administrator'. For this reason, stick with the approaches described above.



来源:https://stackoverflow.com/questions/4403784/install-files-to-original-users-my-docs-folder-via-inno-setup-on-windows-vista

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!