7

将应用程序配置文件添加到Visual Studio中的.Net项目时,它将被命名为app.config,并将重命名(构建时)为ApplicationName.configVisual Studio可以像app.config一样自动调整其他文件的名称吗?

我有一个约40个项目的解决方案。我想将log4net功能添加到其中的一些功能中。所以对于每个项目我都会添加一个文件app.log4net。然后,我会宣布这样的后生成事件:

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

这工作得很好。但是我想知道是否有内置的方式来实现同样的功能,而不需要一个明确的构建后事件。

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

回答

6

在这种情况下,它不是Visual Studio更新app.config的名称,而是它是独立于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> 
相关问题