Can Visual Studio automatically adjust the name of other file as it does with app.config?

不羁岁月 提交于 2019-12-19 02:05:48

问题


When adding an Application Configuration file to a .Net project in Visual Studio it will be named app.config and will be renamed (on build) to ApplicationName.config.

I have a solution with about 40 projects. I want to add log4net functionality to quite a few of them. So for every project I would add a file app.log4net. I would then declare a post-build event like this:

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

That works just fine. But I was wondering whether there was a built-in way to achieve the same without an explicit post-build event.

Edit: While I like both solutions proposed by JaredPar and Simon Mourier, they don't provide what I was hoping for. Having a custom tool or MsBuild rule for this makes it less transparent (for other programmers on the project) or at least more complicated than using the post-build event I am currently using. Nevertheless, I feel like MsBuild would be the correct place to solve similar issues.


回答1:


In this case it's not Visual Studio which is updating the name of app.config but instead it's a core MSBuild rule which is independent of Visual Studio. If you want to emulate the app.config model this is the approach you should take

The two parts of the build sequence which control the copying of app.config are found in Microsoft.Common.targets.

First the name of the file is calculated

<ItemGroup>
    <AppConfigWithTargetPath Include="$(AppConfig)" Condition="'$(AppConfig)'!=''">
        <TargetPath>$(TargetFileName).config</TargetPath>
    </AppConfigWithTargetPath>
</ItemGroup>

Next it is actually copied as a part of the build

<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>



回答2:


I think it's pretty hardcoded for app.config (you can try other names xxx.config and it does not work).

You could achieve the same result without a post build event, but with a custom tool that you would choose for your .log4net files. See these examples: Writing a custom tool to generate code for Visual Studio .NET and Developing a Visual Studio Custom Tool



来源:https://stackoverflow.com/questions/5994244/can-visual-studio-automatically-adjust-the-name-of-other-file-as-it-does-with-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!