Setting connections strings in .config files with WiX

This is usually a common feature request: to be able to modify a connection string in a .config file while installation with wix installer. The most simple thing to do is use a XmlFile( be careful as documentation is from 2.0 version) element.

It’s best to download the most current version of WiX before starting playing around with XmlFile from Some earlier versions didn’t include variables with error messages.

Now for the wxs file. If you are using Votive project in visual studio, be sure to add reference to WixUtilExtension.dll – it contains routines for xml manipulation among other things. Another important thing is to add namespace declaration: xmlns:util=”” in the WiX element. Now we can go ahead and use XmlConfig:

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

    2 <Wix xmlns= xmlns:util=>

    3   <Product Id=152ab043-f24c-4576-8127-a10fbd03faa2 Name=PUT-PRODUCT-NAME-HERE Language=1033 Version= Manufacturer=PUT-COMPANY-NAME-HERE UpgradeCode=8c72b0dd-a539-45aa-a2b4-5a4891111acf>

    4     <Package InstallerVersion=200 Compressed=yes />

    5     <Media Id=1 EmbedCab=yes />

    6     <Property Id=SQLSERVER Value=(local)/>

    7     <Property Id=SQLDATABASE Value=Northwind/>

    8     <Property Id=SQLUSER Value=sa/>

    9     <Property Id=SQLPASSWORD/><!–this implies an empty password–>

   10     <Directory Id=TARGETDIR Name=SourceDir>

   11       <Directory Id=ProgramFilesFolder>


   13           <Component Id=ProductComponent Guid=7809a7d4-cd0c-4b66-aaff-53f65e9db038>

   14             <File Id=_F86C7B1F61C44480BEFA68A2D045D7BB Name=consoleapplication1.exe.config src=../ConsoleApplication1/bin/Debug/consoleapplication1.exe.config Vital=yes DiskId=1/>

   15             <util:XmlFile Id=ModifyServiceLocation Action=setValue ElementPath=/configuration/connectionStrings/add[\[]@name=’cs'[\]]/@connectionString File=[INSTALLLOCATION]\consoleapplication1.exe.config Value=Data Source=[SQLSERVER];Initial Catalog=[SQLDATABASE];User Id=[SQLUSERNAME];Pwd=[SQLPASSWORD]/>

   16           </Component>

   17         </Directory>

   18       </Directory>

   19     </Directory>

   20     <Feature Id=ProductFeature Title=PUT-FEATURE-TITLE-HERE Level=1>

   21       <ComponentRef Id=ProductComponent />

   22     </Feature>

   23   </Product>

   24 </Wix>

ElementPath is a XPath of the element to be modified. Note that this is a formatted field and therefore, square brackets in the XPath must be escaped. Action contains operations to be undertaken in this example I decided only to set value of the connection string so it has to be already declared in .config file. Connection string in this example is defined only based on properties for simplicity sake. Also note that properties USERNAME and PASSWORD are used by the Windows installer engine so be carefull no to overwrite them.

3 thoughts on “Setting connections strings in .config files with WiX

  1. zibalas says:

    Won’t windows intaller fire up repair action if you launch your application using advertised shortcut after .config is modified? And how do you ensure that properties are saved and restored during repair?

  2. vedran mimic says:

    Hi, I tried to use util:XmlFile element from your example to set connection string in .config file, but WiX won’t build my project (which has been valid until adding this element); it says “Unresolved reference to symbol ‘CustomAction:SchedXmlFile’ in section…..” and that symbol doesn’t exist in my project. Do you know how could I solve that problem?
    thanks, vedran

  3. […] Setting connections strings in .config files with WiX […]

Comments are closed.