简体   繁体   English

如何在预构建事件中更新.config项目值

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

I have an app.config file that (initially) looks like this: 我有一个(最初)如下所示的app.config文件:

<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. 我需要一种在预构建事件中更新'[MySetting]'值的方法,而无需将另一个.config文件复制到该文件中。 Is there any way to do that? 有什么办法吗?

you can use powershell script that will replace the 'templates' with desired values. 您可以使用Powershell脚本将所需的值替换为“模板”。

[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 (ie 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 ie Cross-platform command line script (eg .bat and .sh) 如果要混合使用linux和Windows等构建环境,请确保您有一个既可以在两个系统上运行的脚本(例如python),也可以使用powershell或(xor ^ _ ^)sed,但可以使用cmd在两个系统上运行在linux上被忽略的标签-有关更多信息,请参见跨平台命令行脚本(例如.bat和.sh)

example: 例:

:; someLinuxCommand.sh
:; exit
windowsCommandHere.cmd

Single script to run in both Windows batch and Linux Bash? 要在Windows批处理和Linux Bash中运行的单个脚本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM