请参阅WiX 和 DTF:在 Vista 中使用引导程序强制提升权限如何运行整个提升的 msi。
您可以借助 GenerateBootstrapper 任务在 .wixproj 文件中自动执行此操作。总结一下:
像这样创建一个 setup.manifest:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="Setup" type="win32" />
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
并像这样修改您的 .wixproj 文件:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- standard PropertyGroups and ItemGroups -->
<PropertyGroup>
<WindowsSDK>$(registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows@CurrentInstallFolder)</WindowsSDK>
</PropertyGroup>
<PropertyGroup Condition="$(WindowsSDK) == ''">
<WindowsSDK>$(registry:HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft SDKs\Windows@CurrentInstallFolder)</WindowsSDK>
</PropertyGroup>
<PropertyGroup>
<mt_exe>$(WindowsSDK)bin\mt.exe</mt_exe>
</PropertyGroup>
<ItemGroup>
<BootstrapperFile Include="Microsoft.Windows.Installer.3.1" >
<ProductName>Windows Installer 3.1</ProductName>
</BootstrapperFile>
<!-- more BootstrapperFile items -->
</ItemGroup>
<Target Name="Bootstrapper"
Inputs="$(OutDir)$(TargetFileName)"
Outputs="$(OutDir)\Setup.exe"
Condition=" '$(OutputType)'=='package' " >
<GenerateBootstrapper ApplicationName="application name"
ApplicationFile="$(TargetFileName)"
BootstrapperItems="@(BootstrapperFile)"
ComponentsLocation="Relative"
OutputPath="$(OutputPath)"
Culture="en-US"
Path="$(WindowsSDK)\Bootstrapper" />
</Target>
<Target Name="PatchSetupExe" DependsOnTargets="Bootstrapper">
<Exec Command='"$(mt_exe)" -manifest setup.manifest -outputresource:$(OutDir)\Setup.exe;#1' IgnoreExitCode='false' />
</Target>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WiX\v3.0\Wix.targets" />
<PropertyGroup>
<BuildDependsOn>$(BuildDependsOn);Bootstrapper;PatchSetupExe</BuildDependsOn>
</PropertyGroup>
</Project>
现在,每次构建都会生成一个正确的 setup.exe,它将在提升后运行。