Often we need to modify application configuration files during installation of the application. Generally to set some application settings, or modify database connection strings etc. We can do this in WIX by using the <util:XmlFile/> custom actions. To use these custom actions we need to reference WIXUtilExtension to the setup project.
<util:XmlFile Id="UpdateConnectionString" File="[#FileID]" ElementPath="XPATH" Action="setValue" Value="" </util:XmlFile>
The File attribute specifies the ID of the configuration file which is defined using the <File/> element in WIX source file; ElementPath specifies the XPATH to an element in the configuration file which needs to be modified, Action specifies what to do with the element; it should be either createElement, deleteElement, setValue, or bulkSetValue. And the Value specifies the target value to be set to the element. Mostly we would use a Property that has been set by the installer UI sequence as a target value.
Below example shows how to modify the connection string DBConn and the application setting MySetting in the Web.Config file.
<Component Id=”ConfigureWebConfig” Guid=”*”> <File Id=”MyConfigfile” Source=”$(var.Configuration)web.config” KeyPath=”yes” /> <!-- update a connection string --> <util:XmlFile Id="UpdateConnectionString" File="[#MyConfigfile]" Action="setValue" ElementPath= "//configuration/connectionStrings/add[[]@name=’DBConn’[]]/@connectionString" Value="Server=[DBSERVER];Database=[DBNAME];Integrated Security=SSPI;"/> <!-- update an application setting --> <util:XmlFile Id="UpdateMySetting" File="[#MyConfigfile]" Action="setValue" ElementPath="//configuration/appSettings/add[[]@key=’MySetting’[]]/@value" Value="[TARGETVALUE]" /> </Component>
Note that we need to escape the square brackets in XPATH to the element.
Hope it helps.
– Ranjith
Thanks, this is very helpful. My only problem is that it gives an error if there is no ConnectionString in the web.config. I need in that case a connectionstring to be inserted in the web.config. Pls advise.
Hi Ranjith,
I have tried many approaches to update app setting.
None of option worked for me.
Can you please share working sample example which update app setting during installation.
Thanks in advace.