title: WIX execute custom action with admin privilege
body: <p>I have written a custom action for my WIX installer. The action´s Execute-attribute is set to deferred and Impersonate and running before InstallFinalize but it encounters a problem within this action which is the missing admin rigth. The action creates a file in the INSTALLFOLDER which is Program File (x86) </p>
  
  <p>Thats my WIX code:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"&gt;
      &lt;Product Id="*" 
             Name="WixTesterSetup" 
             Language="1033" 
             Version="1.0.0.0" 
             Manufacturer="WixTester" 
             UpgradeCode="77b7ed9a-5394-43e9-aecb-cd9985368ef6"&gt;
          &lt;Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" /&gt;
  
          &lt;MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." /&gt;
          &lt;MediaTemplate /&gt;
  
      &lt;Feature Id="Core" Title="Core" Level="1" ConfigurableDirectory="INSTALLFOLDER" /&gt;
  
      &lt;UI&gt;
        &lt;UIRef Id="WixUI_Minimal" /&gt;
        &lt;Publish  Dialog="ExitDialog"
                  Control="Finish"
                  Event="DoAction"
                  Value="LaunchApplication"&gt;WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1 and NOT Installed&lt;/Publish&gt;
      &lt;/UI&gt;
  
      &lt;Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="Launch Wix Tester" /&gt;
      &lt;Property Id="WixShellEecxTarget" Value="[#WixTester.exe]" /&gt;
      &lt;CustomAction Id="LaunchApplication" BinaryKey="WixCA" DllEntry="WixShellExec" Impersonate="yes" /&gt;
  
      &lt;Binary Id="CustomActionBinary" SourceFile="$(var.RegistrationInfoCustomAction.TargetDir)$(var.RegistrationInfoCustomAction.TargetName).CA.dll"/&gt;
      &lt;CustomAction Id="RegistrationInfoCustomAction" BinaryKey="CustomActionBinary" DllEntry="SaveUserInfo" Execute="deferred" Impersonate="no" /&gt;
  
      &lt;InstallExecuteSequence&gt;
        &lt;Custom Action='RegistrationInfoCustomAction' Before='InstallFinalize'&gt;NOT Installed&lt;/Custom&gt;
      &lt;/InstallExecuteSequence&gt;
  
      &lt;Directory Id="TARGETDIR" Name="SourceDir"&gt;
              &lt;Directory Id="ProgramFilesFolder"&gt;
          &lt;Directory Id="INSTALLFOLDER" Name="WixTesterSetup"&gt;
            &lt;Component Feature="Core"&gt;
              &lt;File Id="WixTester.exe" Source="$(var.WixTester.TargetPath)" KeyPath="yes" Checksum="yes"/&gt;
            &lt;/Component&gt;
          &lt;/Directory&gt;
              &lt;/Directory&gt;
          &lt;/Directory&gt;
  
    &lt;/Product&gt;
  
  &lt;/Wix&gt;
  </code></pre>
  
  <p>Simple custom action:</p>
  
  <pre><code>    public class CustomActions
  {
      [CustomAction]
      public static ActionResult SaveUserInfo(Session session)
      {
          File.Create(System.IO.Path.Combine(session.GetTargetPath("INSTALLFOLDER"), "test.xml"));
  
          return ActionResult.Success;
      }
  }
  </code></pre>
  
  <p>Not intresting WixTester:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Console.WriteLine("Test Started");
          Console.ReadLine();
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Re15J.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Re15J.png" alt="enter image description here"></a></p>