从WIX安装程序更新app.config?

5

我正在尝试使用Wix 3.6,现在它的外观如下:

<?xml version="1.0" encoding="UTF-8"?>

<Directory Id="TARGETDIR" Name="SourceDir">
    <Directory Id="myappINSTALLDIR" Name="myapp5Service">
        <Component Id="SampleServiceComponent" DiskId="1" Guid="6f51c0f3-776c-4aec-a200-1f199352c6c3" Win64="yes">
          <File Id="myapp5.WindowsService.exe" Name="myapp5.WindowsService.exe" Source="$(var.myapp.WindowsService.TargetDir)\myapp5.WindowsService.exe" KeyPath='yes'/>
          ...

          <ServiceInstall Id="InstallmyappService" DisplayName="myappService" Name="myapp5.WindowsService.exe" Description="myapp 5 Service - För effektivare och enklare operationsplanering" Account="LocalSystem" ErrorControl="normal" Start="auto" Type="ownProcess" Vital="yes" />
          <ServiceControl Id="ControlmyappService" Name="myapp5.WindowsService.exe" Start="install" Stop="both" Remove="uninstall" Wait="yes" />
        </Component>
    </Directory>
</Directory>


<WixVariable Id="WixUIBannerBmp" Value="$(var.ProjectDir)\Image\myappTopBanner.bmp" />
<WixVariable Id="WixUIDialogBmp" Value="$(var.ProjectDir)\Image\myappDialogBackground.bmp" />

<Property Id="WIXUI_INSTALLDIR" Value="myappINSTALLDIR" />
<UIRef Id="WixUI_InstallDir" />

<Feature Id="ProductFeature" Title="Wix_myapp.WindowsService" Level="1">
  <ComponentRef Id="SampleServiceComponent" />
</Feature>
<Media Id="1" Cabinet="SampleServiceComponent.cab" EmbedCab="yes" />
</Product>

现在我需要在Wix安装程序中添加一个对话框,用于设置一个appSetting和一个baseadress(WCF)到app.config文件中。这必须在安装之前完成,因为它将决定Wix安装的Windows服务的名称。

最好附上一个示例!

编辑1:

    <WixVariable Id="WixUIBannerBmp" Value="$(var.ProjectDir)\Image\myappTopBanner.bmp" />
<WixVariable Id="WixUIDialogBmp" Value="$(var.ProjectDir)\Image\myappDialogBackground.bmp" />

<Property Id="SERVICEADDRESS" Value="http://serviceaddress"/>
<Property Id="WIXUI_INSTALLDIR" Value="myappINSTALLDIR" />
<UIRef Id="WixUI_InstallDir" />

<util:XmlFile Id="UpdateBaseAddress"
  Action="setValue"
  File="$(var.myapp.WindowsService.TargetDir)\myapp5.WindowsService.exe.config"
  SelectionLanguage="XPath"
  Permanent="yes"
  ElementPath="/configuration/applicationSettings/ServiceName"
  Name="baseAddress" Value="[SERVICEADDRESS]" />

<Feature Id="ProductFeature" Title="Wix_myapp.WindowsService" Level="1">
  <ComponentRef Id="SampleServiceComponent" />
</Feature>
<Media Id="1" Cabinet="SampleServiceComponent.cab" EmbedCab="yes" />
</Product>

1个回答

10

您可以将WixUtilExtension.dll引用添加到安装程序项目中,然后使用XmlFile更新app.config,如下所示:

<Property Id="SERVICEADDRESS" Value="http://serviceaddress"/>

<util:XmlFile Id="UpdateBaseAddress" 
  Action="setValue" 
  File="[DirApplication]$(var.app.config)"
  SelectionLanguage="XPath" 
  Permanent="yes"
  ElementPath="/configuration/applicationSettings/...."
  Name="baseAddress" Value="[SERVICEADDRESS]" />

请注意,您需要设置目录和.config文件的名称(您可以使用$(var.ProjectName.TargetFileName).config,它应该会自动为您解决此问题)。


谢谢!请看编辑1。我已经更新了我的项目,但是我遇到了以下异常:产品元素包含一个意外的子元素。还要注意,在安装服务之前,用户将此信息设置到配置文件中非常重要(它决定了安装的Windows服务的名称)。 - Banshee
4
根据文档XmlFile的要求,必须将其放置在Component元素中。 - Dialecticus
5
请注意,要使用丹尼尔的答案,您需要在根<Wix />元素中添加“xmlns:util =”http://schemas.microsoft.com/wix/UtilExtension"”,以定义'util'。 - Jesus is Lord
1
如果这是你的应用程序设置 <appSettings><add key="MyAppSetting" value="OldValue"/></appSettings>,那么正确的转义应该是 ElementPath="/configuration/appSettings/add[\[]@key='MyAppSetting'[\]]" Name="value" Value="NewValue" - Jesus is Lord
1
这个如何处理升级? - Fowl
2
不清楚的是何时触发此更改。这只会在安装过程结束时发生吗? - Fabricio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接