dcarneiro
dcarneiro

Reputation: 7150

Multiple config files for console applications

There's already a similar topic but it don't answer my question.

When developing web applications in visual studio 2010, you can have multiple configuration files. You have the default Web.config and then you can have a Web.Debug.config and a Web.Release.config.

Now lets build a C# console application and name it Foo. The default config file should be Foo.exe.Config. Can we override this config file for different environments? How should we call it? Foo.exe.Release.config or Foo.Release.exe.config?

Upvotes: 4

Views: 8134

Answers (2)

Aliz
Aliz

Reputation: 756

This post explains in 8 steps how to do it: https://mitasoft.wordpress.com/2011/09/28/multipleappconfig/

It solved the problem for me.

Update: I add below the steps in case if the article is removed (thx @A.V :) )

  1. Prepare your project and add app.config, app.debug.config and app.release.config. Make sure is running under .Net 4.0 .

  2. Right click on the project, click Unload project and then Edit .csproj.

  3. Below the last PropertyGroup add the following:

<PropertyGroup>
  <ProjectConfigFileName>App.config</ProjectConfigFileName>
</PropertyGroup>
  1. Modify the section ItemGroup that’s related to app.config/app.*.config files
<ItemGroup>
   <None Include="App.config" />
   <None Include="App.Debug.config">
     <DependentUpon>App.config</DependentUpon>
   </None>
   <None Include="App.Release.config">
     <DependentUpon>App.config</DependentUpon>
   </None>
 </ItemGroup>
  1. Below the last Import tag insert this one

< Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />

  1. Right before end of Project tag add this
<Target Name="AfterBuild">
  <TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)"

Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />

  1. Now you can save the project, right click the project and choose Reload Project.

  2. For app.debug.config / app.release.config files you can use the template that’s provided for web projects, which looks like this below

<?xml version="1.0"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <appSettings>
    <add key="Mode" value="Debug" xdt:Transform="Insert"/>
  </appSettings>
  <!--
    In the example below, the "SetAttributes" transform will change the value of 
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator 
    finds an atrribute "name" that has a value of "MyDB".

    <connectionStrings>
      <add name="MyDB" 
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
  -->
  <system.web>
    <!--
      In the example below, the "Replace" transform will replace the entire 
      <customErrors> section of your web.config file.
      Note that because there is only one customErrors section under the 
      <system.web> node, there is no need to use the "xdt:Locator" attribute.

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
</configuration>

Upvotes: 2

Brian Ball
Brian Ball

Reputation: 12596

There is no native support for this in VS2010, but you can check out this blog: http://vishaljoshi.blogspot.com/2010/05/applying-xdt-magic-to-appconfig.html

Upvotes: 3

Related Questions