Can we have multiple App.Config files in .NET console application?

14,775

Solution 1

UPDATE

With Visual Studio 2010 and 2012, you this has all been integrated into the IDE. If you right click your config file, VS give you the option to generate a transform config for each of your build configurations. If you were to create a build configuration for each of your environments, MSBuild will automatically generate the correct Web.config/app.config for you.

Short answer, yes. You can have the different files and in your build script, but you'll have to rename the correct one to "App.config" and you're set (before you compile).

Long answer, what you should be using is the Enterprise Library MergeConfiguration tool. This allows you to use your existing App.config as the base and define deltas per environment. The tool will merge the base and the delta to generate the environment-specifig config files. You will still need some logic in a build script to apply the correct config file.

When you install Enterprise Library on your machine, you can right click the config file in Visual Studio and edit it via the config tool. You can use that to define your environments and the app settings and connection strings to override per environment.

http://entlib.codeplex.com/

Solution 2

To follow on from Babak's answer, you could also separate parts of your config out into other config files by using the configSource attribute on any element which represents a ConfigurationSection, e.g.:

<appSettings configSource="appSettings.config" />

And in appSettings.config:

<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
    <add key="Blah" value="Nim nim nim" />
</appSettings>
Share:
14,775
Rita
Author by

Rita

Updated on June 07, 2022

Comments

  • Rita
    Rita almost 2 years

    I have a console application that has App.Confile file. Now the parameters that are environment specific are maintained here.

    Now I am thinking to have multiple app.config files (like app.dev.config, app.test.config and app.prod.config) the way how we can have multiple Web.Config files.

    In case of Web application, we can handle this and ConfigurationManager would pick respective Web.Config file.

    In case of Console application, I am not sure. If Yes, how can we have multiple app.config files?

    Appreciate your help.

    Thanks

  • Babak Naffas
    Babak Naffas over 13 years
    Will this handle connection string entries?
  • Matthew Abbott
    Matthew Abbott over 13 years
    You can do: <connectionStrings configSource="connectionStrings.config" />
  • Rita
    Rita over 13 years
    This worked. I have put copy $(ProjectDir)app.$(ConfigurationName).config $(ProjectDir)app.config in the Build Command.