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

后端 未结 14 2711
走了就别回头了
走了就别回头了 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:18

    I tried several solutions and here is the simplest I personally found.
    Dan pointed out in the comments that the original post belongs to Oleg Sych—thanks, Oleg!

    Here are the instructions:

    1. Add an XML file for each configuration to the project.

    Typically you will have Debug and Release configurations so name your files App.Debug.config and App.Release.config. In my project, I created a configuration for each kind of environment, so you might want to experiment with that.

    2. Unload project and open .csproj file for editing

    Visual Studio allows you to edit .csproj files right in the editor—you just need to unload the project first. Then right-click on it and select Edit .csproj.

    3. Bind App.*.config files to main App.config

    Find the project file section that contains all App.config and App.*.config references. You'll notice their build actions are set to None:

    
    
    
    

    First, set build action for all of them to Content.
    Next, make all configuration-specific files dependant on the main App.config so Visual Studio groups them like it does designer and code-behind files.

    Replace XML above with the one below:

    
    
      App.config
    
    
      App.config
    
    

    4. Activate transformations magic (only necessary for Visual Studio versions pre VS2017)

    In the end of file after

    
    

    and before final

    
    

    insert the following XML:

      
      
        
        
        
        
          
          
            $(TargetFileName).config
          
        
      
    

    Now you can reload the project, build it and enjoy App.config transformations!

    FYI

    Make sure that your App.*.config files have the right setup like this:

    
    
         
    
    

提交回复
热议问题