Welcome to WindowsClient.net | Sign in | Join

Zuker On Foundations

The realm of .NET (WPF, WCF and all around)

Managing Multiple Configuration File Environments with Pre-Build Events

Almost every project I write has a configuration file and its content varies from one environment to another.
In this case, managing multiple configuration files can make life a lot easier.

There are different approaches for doing this, I'll link to a demonstration made by Scott Hanselman a while back - Click Here.

In short:
1 - Create configuration builds.
2 - Add a batch file at the root of the project as written below.
3 - Add a Pre-Build action to execute it.

The batch file 'copyifnewer.bat' content:
@echo off
echo Comparing two files: %1 with %2

if not exist %1 goto File1NotFound
if not exist %2 goto File2NotFound

fc %1 %2
if %ERRORLEVEL%==0 GOTO NoCopy

echo Files are not the same.  Copying %1 over %2
copy %1 %2 /y & goto END

:NoCopy
echo Files are the same.  Did nothing
goto END

:File1NotFound
echo %1 not found.
goto END

:File2NotFound
copy %1 %2 /y
goto END

:END
echo Done.

The pre-build action to be inserted:
"$(ProjectDir)copyifnewer.bat" "$(ProjectDir)web.config.$(ConfigurationName)" "$(ProjectDir)web.config"

 

Posted: Jun 29 2008, 08:06 AM by zuker | with 1 comment(s)
Filed under:

Comments

Schalk said:

For interest sake, have a look at how Castle Windsor does this using XML processing instructions in the config file - a different way of doing things...

www.castleproject.org/.../defines.html

<configuration>

   <?define DEBUG?>

   <components>

   <?if DEBUG?>

       <component

           id="my.component"

           service="Namespace.IService, AssemblyName"

           type="DebugVersion, AssemblyName" />

   <?elsif STAGE?>

       <component

           id="my.component"

           service="Namespace.IService, AssemblyName"

           type="DebugVersion, AssemblyName" />

   <?elsif PRODUCTION?>

       <component

           id="my.component"

           service="Namespace.IService, AssemblyName"

           type="DebugVersion, AssemblyName" />

   <?else?>

       <component

           id="my.component"

           service="Namespace.IService, AssemblyName"

           type="DebugVersion, AssemblyName" />

   <?end?>

   </components>

   <?undef DEBUG?>

</configuration>

# July 15, 2008 12:16 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Page view counter