How can I modify web.config after install?

Oct 8, 2015 at 11:28 AM
In WiX I did it regarding the UtilExtension for example:
<?xml version="1.0" encoding="UTF-8"?>
<?include ..\Variables.wxi ?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">

    <Fragment>
        <DirectoryRef Id="INSTALLLOCATION">
            <Component Id="ChangeAppSettings.App" Guid="70D7CC9F-EA46-45B1-857F-F07469F18139" KeyPath="yes">
              <util:XmlFile Id="ModifyUseCustomerAsUser.App" Action="setValue" Permanent="yes" File="[INSTALLLOCATION]Web.config"
                            ElementPath="/configuration/appSettings/add[\[]@key='UseCustomerAsUser'[\]]/@value"
                            Value="true" />
              <util:XmlFile Id="ModifyApprovalSystemPriceLimit.App" Action="setValue" Permanent="yes" File="[INSTALLLOCATION]Web.config"
                            ElementPath="/configuration/appSettings/add[\[]@key='ApprovalSystemPriceLimit'[\]]/@value"
                            Value="0" />
            </Component>
        </DirectoryRef>
    </Fragment>

    <Fragment>
        <ComponentGroup Id="ChangeConfig.App">
            <ComponentRef Id="ChangeAppSettings.App"/>
        </ComponentGroup>
    </Fragment>

</Wix>
I understood that I can do it in Custom Action, but maybe it is possible somehow by default way in Wix#?
Coordinator
Oct 8, 2015 at 11:53 PM
Edited Oct 8, 2015 at 11:57 PM
While of course you can achieve the above WXS using Wix# Fragments or even direct XML injections it's not necessarily the best way for every situation.

Wix# allows using more comprehensive XML manipulation techniques. The DeferredActions sample demonstrates 4 different techniques of manipulating the *.config file content (e.g. setting DB connection string).

On of the techniques is using Wix# CommonTasks makes and it is quite similar to a typical WiX declarative syntax:
string configFile = Path.Combine(session.Property("INSTALLDIR"), "MyApp.exe.config");

Tasks.SetConfigAttribute(configFile, "//configuration/appSettings/add[@key='AppName']/@value", "My App");
Tasks.SetConfigAttribute(configFile, "//configuration/connectionStrings/add[@name='Server1']/@connectionString", "DataSource=(localdb)/v11.0;IntegratedSecurity=true");
Tasks.SetConfigAttribute(configFile, "//configuration/connectionStrings/add[@name='Server1']/@providerName", "System.Data.SqlClient");