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

前端 未结 3 1751
-上瘾入骨i
-上瘾入骨i 2020-12-06 13:10

I have a program that needs to create files in the My Document directory on installation. This is a strict fixed requirement, there is no changing this. Problem is that if t

相关标签:
3条回答
  • 2020-12-06 13:16

    The answer to the original is valid but not recomended. When the setup is run, RunAsOriginalUser will run as the user currently logged into Windows. This is done by having part of the setup run unelevated, and then run another copy that is elevated to do the actual install.

    When the user explicitly does "Run as admin", the "unelevated stub" runs elevated as well, in which case, there is nothing the setup can do to access the original user as that information has already been replaced.

    The accepted practice is to do any profile specific work in the application itself as you suggested, which also means it will work for other users and in a LUA environment in pre Vista (Where you would have had exactly the same situation that you're seeing now).

    0 讨论(0)
  • 2020-12-06 13:27

    This article might provide you some guidance on how to work around the elevation,

    http://www.codeproject.com/Articles/18946/High-elevation-can-be-bad-for-your-application-How

    Generally speaking, it gives you a chance to execute your executable (which creates files under My Documents) at the end of installation without elevation.

    0 讨论(0)
  • 2020-12-06 13:40

    First, make sure the installer won't require privileges elevation on its own, by setting PrivilegesRequired=lowest:

    [Setup]
    PrivilegesRequired=lowest
    

    To abort the installer, when the installer is running "As Administrator" explicitly by the user, on Windows Vista and older, use IsAdmin function (or IsAdminInstallMode or IsAdminLoggedOn [in older versions]) in InitializeSetup:

    [Code]
    
    function InitializeSetup(): Boolean;
    begin
      Result := True;
    
      if (GetWindowsVersion >= $05010000) and
         IsAdmin then
      begin
        MsgBox('Do not run this installer "As Administrator".', mbError, MB_OK);
        Result := False;
      end;
    end;
    

    For more discussion on the topic, see Install files to original user's My Docs folder via Inno Setup on Windows Vista/7.

    0 讨论(0)
提交回复
热议问题