I have two application config file (app.debug.config and app.production.config), I found this solution to copy the config file to output folder according to the current build configuration name:
<Target Name="AfterBuild">
<Delete Files="$(TargetDir)$(TargetFileName).config" />
<Copy SourceFiles="$(ProjectDir)\App.$(Configuration).config" DestinationFiles="$(TargetDir)$(TargetFileName).config" />
</Target>
so after selecting for example the (Production) build configuration, the MSbuild will automatically copy the app.production.config and rename it to projectname.config in the output folder.
unfortunately it is not the same case for publishing, because when I published the project to a web server, the configuration file is not published.
how can I do the same task for publishing?
I found out the solution, I added the following to the project file:
<ItemGroup>
<CustomConfigFile Include="$(ProjectDir)\App.$(Configuration).config">
<Visible>False</Visible>
</CustomConfigFile>
</ItemGroup>
<Target Name="BeforePublish">
<CreateItem Include="@(CustomConfigFile)" AdditionalMetadata="TargetPath=$(TargetFileName).config;IsDataFile=false">
<Output TaskParameter="Include" ItemName="_DeploymentManifestFiles" />
</CreateItem>
</Target>