Visual Studio能否像对待app.config文件那样自动调整其他文件的名称?

7

在Visual Studio的.Net项目中添加应用程序配置文件时,它将被命名为app.config并在构建时重命名为ApplicationName.config

我有一个包含大约40个项目的解决方案。我想为其中很多项目添加log4net功能。因此,对于每个项目,我都会添加一个app.log4net文件。然后我会声明一个后置构建事件,如下所示:

copy $(ProjectDir)app.log4net $(TargetPath).log4net

这很好用。但我想知道是否有一种内置的方式来实现相同的功能,而不需要显式的后构建事件。

编辑:虽然我喜欢JaredPar和Simon Mourier提出的两种解决方案,但它们并没有提供我所希望的。为此编写自定义工具或MsBuild规则会使其不够透明(对于项目中的其他程序员)或至少比我当前使用的后构建事件更加复杂。尽管如此,我觉得MsBuild是解决类似问题的正确地方。

2个回答

6
在这种情况下,更新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>

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接