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.
On the menu bar, choose Project > Add New Item. The Add New Item dialog box appears. Expand Installed > Visual C# Items, and then choose the Application Configuration File template. In the Name text box, enter a name, and then choose the Add button. A file named app.config is added to your project.
To modify user settings, you'll use the Settings editor to review and change VS Code settings. To open the Settings editor, use the following VS Code menu command: On Windows/Linux - File> Preferences> Settings On macOS - Code> Preferences> Settings
When the user adds an item (like class, or interface) to the project in Visual Studio, IDE automatically sets default namespace. Each assembly has default namespace, and in case of sub folders within the project, directory structure is reflected in the namespace. At the beginning everything is clean, and well organized.
Press Ctrl +. to trigger the Quick Actions and Refactorings menu and select Rename file to TypeName.cs from the Preview window popup, where TypeName is the name of the type you have selected. Press Ctrl +. to trigger the Quick Actions and Refactorings menu and select Rename type to Filename from the Preview window popup,...
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With