App.Config Transformation for projects which are not Web Projects in Visual Studio?

后端 未结 14 2533
走了就别回头了
走了就别回头了 2020-11-22 04:06

For Visual Studio 2010 Web based application we have Config Transformation features by which we can maintain multiple configuration files for different environments. But the

14条回答
  •  情深已故
    2020-11-22 04:22

    In my experience, the things I need to make environment-specific are things like connection strings, appsettings and often smpt settings. The config system allows to specify these things in separate files. So you can use this in your app.config/web.config:

     
     
     
        
           
        
     
    

    What I typically do is to put these config-specific sections in separate files, in a subfolder called ConfigFiles (either in the solution root or at the project level, depends). I define a file per configuration, e.g. smtp.config.Debug and smtp.config.Release.

    Then you can define a pre-build event like so:

    copy $(ProjectDir)ConfigFiles\smtp.config.$(ConfigurationName) $(TargetDir)smtp.config
    

    In team development, you can tweak this further by including the %COMPUTERNAME% and/or %USERNAME% in the convention.

    Of course, this implies that the target files (x.config) should NOT be put in source control (since they are generated). You should still add them to the project file and set their output type property to 'copy always' or 'copy if newer' though.

    Simple, extensible, and it works for all types of Visual Studio projects (console, winforms, wpf, web).

提交回复
热议问题