Search code examples
c#bashvisual-studiopre-build-event

How to update .config item value in pre-build event


I have an app.config file that (initially) looks like this:

<configuration>
 <appSettings>
   <add key="MySetting" value="[MySetting]" />
 </appSettings>
</configuration>

I need a way to update '[MySetting]' value in pre-build event without copying another .config file into this one. Is there any way to do that?


Solution

  • you can use powershell script that will replace the 'templates' with desired values.

    [IO.File]::ReadAllText("$DIR\app.template.config").Replace("[MySetting]", $MySettingValue) | Set-Content "$DIR\app.config"
    

    Update

    If you want to mix build environments - linux and windows, make sure that you will have a script that either run on both systems (i.e. python) or use powershell or (xor ^_^) sed but make it runnable from both systems using cmd label that is ignored on linux - for more info see i.e. Cross-platform command line script (e.g. .bat and .sh)

    example:

    :; someLinuxCommand.sh
    :; exit
    windowsCommandHere.cmd
    

    Single script to run in both Windows batch and Linux Bash?