Search code examples
.netapp-configslowcheetah

.NET App.config. How to replace file attribute?


Here is short summary of current situation: In my solution (console application) I use app.config file with entry like this:

< appSettings file="Additional.config"> On our production environment this file is provided by separate routine and we don't want to put it in projects file repository. Still we need a convenient way to run the solution in dev environment.

We've come with an idea to replace the file attribute using SlowCheetach. It works just fine with replacing key-value entries but we are unable to make it run correctly.

Is there a way to replace file property with SlowCheetach or any other lightweight tool? We really want to avoid using build scripts to modify app.config output.


Solution

  • Solution below is not working properly because id is not a valid schema element. Real solution at the bottom.

    I've figured out a simple way to accomplish this. All that is needed to do is add another attribute to appSettings and set it as Match Selector so entry in app.config would look like:

     <appSettings file="Additional.config" id="appconfig">
    

    and the one in App.Debug.config:

    <appSettings id="appconfig" file="New/Path/To/Additional.config" xdt:Transform="Replace" xdt:Locator="Match(id)">
    

    Real solution here:

    It turns out that you can simply use transform in form:

       <appSettings xdt:Transform="RemoveAttributes(file)">
    

    which will remove only attributes passed to RemoveAttributes and leave rest of the config intact.

    More about possible transform attributes can be read here