将应用程序配置文件添加到 Visual Studio 中的 .Net 项目时,它将被命名为app.config
并将被重命名(在构建时)为ApplicationName.config
.
我有一个包含大约 40 个项目的解决方案。我想为其中一些添加 log4net 功能。所以对于每个项目我都会添加一个文件app.log4net
。然后我会声明一个像这样的构建后事件:
copy $(ProjectDir)app.log4net $(TargetPath).log4net
效果很好。但我想知道是否有一种内置方法可以在没有明确的构建后事件的情况下实现相同的目的。
编辑:虽然我喜欢 JaredPar 和 Simon Mourier 提出的两种解决方案,但它们没有提供我所希望的。为此使用自定义工具或 MsBuild 规则使其不太透明(对于项目中的其他程序员而言),或者至少比使用我当前使用的构建后事件更复杂。尽管如此,我觉得 MsBuild 会是correct解决类似问题的地方。
在这种情况下,更新 app.config 名称的不是 Visual Studio,而是独立于 Visual Studio 的核心 MSBuild 规则。如果您想模拟 app.config 模型,这是您应该采取的方法
控制 app.config 复制的构建序列的两个部分位于 Microsoft.Common.targets 中。
首先计算文件名
<ItemGroup>
<AppConfigWithTargetPath Include="$(AppConfig)" Condition="'$(AppConfig)'!=''">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
接下来它实际上被复制为构建的一部分
<Target
Name="_CopyAppConfigFile"
Condition=" '@(AppConfigWithTargetPath)' != '' "
Inputs="@(AppConfigWithTargetPath)"
Outputs="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')">
<!--
Copy the application's .config file, if any.
Not using SkipUnchangedFiles="true" because the application may want to change
the app.config and not have an incremental build replace it.
-->
<Copy
SourceFiles="@(AppConfigWithTargetPath)"
DestinationFiles="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')"
OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"
Retries="$(CopyRetryCount)"
RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)"
UseHardlinksIfPossible="$(CreateHardLinksForAdditionalFilesIfPossible)"
>
<Output TaskParameter="DestinationFiles" ItemName="FileWrites"/>
</Copy>
</Target>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)