2

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?

user2081328
  • 159
  • 3
  • 15

1 Answers1

1

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?

Miq
  • 3,931
  • 2
  • 18
  • 32
  • Thanks for the quick reply. I have no experience in using powershell scripts unfortunately, a just some experience in using pre and post build events. How do I use this in my pre-build event? – user2081328 Apr 05 '18 at 10:48
  • create a ps1 script and execute it - see here: https://stackoverflow.com/questions/6500320/post-build-event-execute-powershell – Miq Apr 05 '18 at 10:51
  • Thanks, this works great, but unfortunately does not solve my problem. Project must build in app center and it does not support powershell :(. Anyway for me to do this using bash? – user2081328 Apr 05 '18 at 12:15