I tried several solutions, and here is the simplest I've found.
Dan pointed out in the comments that the original post belongs to Oleg Sych—thanks, Oleg!
Dan pointed out in the comments that the original post belongs to Oleg Sych—thanks, Oleg!
Here are the instructions:
1. Add an XML file for each configuration to the project.
Typically you will have
Debug
and Release
configurations so name your files App.Debug.config
and App.Release.config
. In my project, I created a configuration for each kind of enironment so you might want to experiment with that.
2. Unload project and open .csproj file for editing
Visual Studio allows you to edit .csproj right in the editor—you just need to unload the project first. Then right-click on it and select Edit .csproj .
3. Bind App.*.config files to main App.config
Find the project file section that contains all
App.config
and App.*.config
references. You'll notice their build actions are set to None
: Include="App.config" />
Include="App.Debug.config" />
Include="App.Release.config" />
First, set build action for all of them to
Next, make all configuration-specific files dependant on the main
Content
.Next, make all configuration-specific files dependant on the main
App.config
so Visual Studio groups them like it does designer and codebehind files.
Replace XML above with the one below:
Include="App.config" />
Include="App.Debug.config" >
App.config
4. Activate transformations magic
In the end of file after
Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
and before final
insert the following XML:
TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
Name="AfterCompile" Condition="exists('app.$(Configuration).config')">
Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
Remove="app.config" />
Include="$(IntermediateOutputPath)$(TargetFileName).config">
$(TargetFileName).config
Now you can reload the project, build it and enjoy
App.config
transformations!
Another solution I've found is NOT to use the transformations but just have a separate config file, e.g. app.Release.config. Then add this line to your csproj file.
Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
App.Release.config
This will not only generate the right myprogram.exe.config file but if you're using Setup and Deployment Project in Visual Studio to generate MSI, it'll force the deployment project to use the correct config file when packaging.
You can use a separate config file per configuration, e.g. app.Debug.config, app.Release.config and then use the configuration variable in your project file:
App.$(Configuration).config
This will then create the correct ProjectName.exe.config file depending on the configuration you are building in.
Комментариев нет:
Отправить комментарий