none
app.config: How to specify configuration dependent values? RRS feed

  • Question

  • Hi!

    I've got an app.config with an appSettings section:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    	<appSettings>
    		<add key="X" value="Y"/>
    	</appSettings>
    </configuration>
    

    Now, ist it possible, to have a different value for X when Visual Studio compiles the project in the DEBUG or Release configureation?

    In the c# Code, I can have a constant defined by visual studio (Project properties->build->define DEBUG constant). Can I have something comparable for the app.config too?

     

    Lots of Greetings!


    Volker
    Monday, May 23, 2011 2:41 PM

Answers

  • You should add below code snippet to your project file:
    ...
     <None Include="App.config" />
     <None Include="App.Release.config">
      <DependentUpon>App.config</DependentUpon>
     </None>
     ...
     </ItemGroup>
     <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
     <UsingTask TaskName="TransformXml"
        AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
     <Target Name="AfterCompile" Condition="exists('app.$(Configuration).config')">
     <TransformXml Source="app.config"
      Destination="$(IntermediateOutputPath)$(TargetFileName).config"
      Transform="app.$(Configuration).config" />
     <ItemGroup>
      <AppConfigWithTargetPath Remove="app.config"/>
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
      <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
     </ItemGroup>
     </Target>
    ...
    </Project>
    

    Make everything as simple as possible, but not simpler.
    • Proposed as answer by Paul Zhou Thursday, May 26, 2011 8:27 AM
    • Marked as answer by Hetzi Friday, May 27, 2011 2:24 PM
    Monday, May 23, 2011 5:22 PM

All replies

  • You can use the "config file transformation".

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
     <appSettings configSource="debug.config ? Release.config"/>
    </configuration>

     


    Make everything as simple as possible, but not simpler.
    • Marked as answer by Hetzi Monday, May 23, 2011 3:44 PM
    • Unmarked as answer by Hetzi Monday, May 23, 2011 4:05 PM
    Monday, May 23, 2011 3:37 PM
  • Hm. A first attempt at doing what the link specified failed becahse the transformXML-Tag wasn't recognized, even after I dealt with the quotes and comments. I'll try again tomorrow.

    Meanwhile, thanks for the link!

    The configsource stuff I don't really like because then I still have to edit the app.config.


    Volker
    Monday, May 23, 2011 4:18 PM
  • You should add below code snippet to your project file:
    ...
     <None Include="App.config" />
     <None Include="App.Release.config">
      <DependentUpon>App.config</DependentUpon>
     </None>
     ...
     </ItemGroup>
     <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
     <UsingTask TaskName="TransformXml"
        AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
     <Target Name="AfterCompile" Condition="exists('app.$(Configuration).config')">
     <TransformXml Source="app.config"
      Destination="$(IntermediateOutputPath)$(TargetFileName).config"
      Transform="app.$(Configuration).config" />
     <ItemGroup>
      <AppConfigWithTargetPath Remove="app.config"/>
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
      <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
     </ItemGroup>
     </Target>
    ...
    </Project>
    

    Make everything as simple as possible, but not simpler.
    • Proposed as answer by Paul Zhou Thursday, May 26, 2011 8:27 AM
    • Marked as answer by Hetzi Friday, May 27, 2011 2:24 PM
    Monday, May 23, 2011 5:22 PM
  • Got it to work.

    Thanks very much!


    Volker
    Friday, May 27, 2011 2:24 PM
  • Check this extension which automate this process

    Configuration Transform

    Saturday, June 9, 2012 6:36 AM