【问题标题】:How to update .config item value in pre-build event如何在预构建事件中更新 .config 项目值
【发布时间】:2018-09-15 03:18:40
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# bash visual-studio pre-build-event


    【解决方案1】:

    您可以使用 powershell 脚本将“模板”替换为所需的值。

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

    更新

    如果您想混合构建环境 - linux 和 windows,请确保您将拥有一个可以在两个系统上运行的脚本(即 python)或使用 powershell 或(xor ^_^)sed 但使其可从两者运行使用在 linux 上被忽略的 cmd 标签的系统 - 有关更多信息,请参阅 Cross-platform command line script (e.g. .bat and .sh)

    示例:

    :; someLinuxCommand.sh
    :; exit
    windowsCommandHere.cmd
    

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

    【讨论】:

    • 感谢您的快速回复。不幸的是,我没有使用 powershell 脚本的经验,只是使用构建前和构建后事件的一些经验。如何在我的预构建活动中使用它?
    • 创建一个 ps1 脚本并执行它 - 见这里:stackoverflow.com/questions/6500320/…
    • 谢谢,这很好用,但不幸的是不能解决我的问题。项目必须在应用程序中心构建,它不支持 powershell :(。无论如何我要使用 bash 来做到这一点?
    猜你喜欢
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    相关资源
    最近更新 更多