single app.config multi-project c#

前端 未结 7 1912
清歌不尽
清歌不尽 2020-11-27 15:10

I want to use a single app.config by 3 different projects.

How to access the configurations?

ConfigurationManager.AppSettings[\"config1\"]

7条回答
  •  隐瞒了意图╮
    2020-11-27 15:33

    I have found the button, and opened the app.config as link, however that caused when build to again create separate config file for each project, and therefore, when deploying the 3 project, i will have 3 config files. What I wanted to do, is keeping a single file for all projects in a certain solution. Can I do that?

    Yes - you can do it, but should you do it?

    The basic assumption in a .NET app is that one app = one config file. Out of the box, and with an easy method, you cannot share config files between applications.

    If you create your own custom config sections, you could "outsource" those to external files, which could be shared. Imagine you create your own custom config section called "MyConfiguration", then your app.config would look something like that:

    
      
        
    10/16/2006 1

    You could have your "MyConfiguration" section in its own file, and reference it from your app's config:

    
      
        

    and your "MyConfiguration.config" would then contain:

       
        
          10/16/2006
          1
        
      
    

    By doing this, you could "externalize" and thus share at least the bulk of your config settings - provided they're in your own custom config sections.

    For more info and an excellent intro to .NET 2.0 and up configuration mysteries, see Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

    • Unraveling the mysteries of .NET 2.0 configuration
    • Decoding the mysteries of .NET 2.0 configuration
    • Cracking the mysteries of .NET 2.0 configuration

    Highly recommended, well written and extremely helpful!

    Marc

提交回复
热议问题