Search code examples
installationwindows-installermachine.config

How do I modify machine.config via an .msi package


I'm trying to create an installer that will deploy a .NET Managed data provider. In order for the data provider to appear as a provider in application drop-downs, I have to add the provider in the machine.config's section:

<system.data>
    <DbProviderFactories>
      <add name="My Data Provider" 
           invariant="Sample.MyDataProvider" 
           description="My Data Provider" 
           type="Eli.Sample.MyDataProvider, Sample.MyDataProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5b9d34470b87a97f" 
      />
    </DbProviderFactories>
</system.data>

How do I do this? Just a pointer would be fine. Thanks.


Solution

  • You can nest the XmlConfig elements for a slightly neater solution. This is what worked for me with WiX toolset v3.11 for .NET Framework 4:

    <util:XmlConfig Id="AddElement32" File="[NETFRAMEWORK40FULLINSTALLROOTDIR]Config\Machine.Config"
                    ElementPath="//configuration/system.data/DbProviderFactories"
                    Action="create" On="install" Node="element"
                    Name="add">
        <util:XmlConfig Id="NameAttribute32" File="[NETFRAMEWORK40FULLINSTALLROOTDIR]Config\Machine.Config" ElementId="AddElement32"
                        Name="name"
                        Value="My Data Provider"
                        />
        <util:XmlConfig Id="InvariantAttribute32" File="[NETFRAMEWORK40FULLINSTALLROOTDIR]Config\Machine.Config" ElementId="AddElement32"
                        Name="invariant"
                        Value="Sample.MyDataProvider"
                        />
        <util:XmlConfig Id="DescriptionAttribute32" File="[NETFRAMEWORK40FULLINSTALLROOTDIR]Config\Machine.Config" ElementId="AddElement32"
                        Name="description"
                        Value="My Data Provider"
                        />
        <util:XmlConfig Id="TypeAttribute32" File="[NETFRAMEWORK40FULLINSTALLROOTDIR]Config\Machine.Config" ElementId="AddElement32"
                        Name="type"
                        Value="Eli.Sample.MyDataProvider, Sample.MyDataProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5b9d34470b87a97f"
                        />
    </util:XmlConfig>
    
    <util:XmlConfig Id="AddElement32Uninstall" File="[NETFRAMEWORK40FULLINSTALLROOTDIR]Config\Machine.Config"
                    Action="delete" On="uninstall" Node="element"
                    ElementPath="//configuration/system.data/DbProviderFactories"
                    VerifyPath="//configuration/system.data/DbProviderFactories/add[\[]@invariant='Sample.MyDataProvider'[\]]"
                    />
    

    Note that although you can now leave out some of the attributes because you're nesting the elements, you still need the File and ElementId attributes.

    The uninstall XmlConfig element seems to need the VerifyPath attribute too, so that it can select the correct element to remove.