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

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

我有一个 app.config 文件(最初)如下所示:

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

我需要一种方法来更新预构建事件中的“[MySetting]”值,而无需将另一个 .config 文件复制到这个文件中。有什么办法吗?

您可以使用 powershell 脚本将 'templates' 替换为所需的值。

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

更新

如果您想混合构建环境 - linux 和 windows,请确保您的脚本在两个系统上都 运行(即 python ) 或使用 powershell 或 (xor ^_^) sed 但使用在 linux 上被忽略的 cmd 标签使其在两个系统中都可 运行nable - 有关更多信息,请参见

示例:

:; someLinuxCommand.sh
:; exit
windowsCommandHere.cmd

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