single app.config multi-project c#

前端 未结 7 1762
清歌不尽
清歌不尽 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:21
    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    config = ConfigurationManager.OpenExeConfiguration(Path.Combine(@"D:\", "config.exe"));
    foreach (string key in config.AppSettings.Settings.AllKeys)
    {
       string value = config.AppSettings.Settings[key].Value;
       ConfigurationManager.AppSettings.Set(key, value);
    }
    
    0 讨论(0)
  • 2020-11-27 15:22

    Let's say you have this folder structure:

    • Solution
      • Project1
      • Project2
      • Project3

    Do this:

    1. Create the App.config file in the Solution level folder. You won't find an option to add an App.config file from the templates, so just create a new empty text file with the name App.config, and paste in the contents of a regular App.config file.
    2. For each project in Solution Explorer:

      1. Right click and select Add > Existing Item
      2. Locate the file
      3. Select Add as link from the drop down box next to the Add button.

    Edited to add:

    You correctly state that the above method only shared the file up to build-time. To use a shared file at run-time, see the answers to this question.

    0 讨论(0)
  • 2020-11-27 15:26

    The common config file

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <section 
                name="appSettings" 
                type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
                />
        </configSections>
        <appSettings>
            <add key="key1" value="value1"/>
        </appSettings>
    </configuration>
    

    To access mapped config file

    ConfigurationFileMap fileMap = new ConfigurationFileMap(file); //Path to your config file
    Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
    string value = configuration.AppSettings.Settings["key1"].Value;
    
    0 讨论(0)
  • 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:

    <configuration>
      <configSections>
        <section name="MyConfiguration" 
                 type="MyConfigurationSection, MyConfigurationAssembly" />
      </configSections>
    
      <MyConfiguration>
        <nestedElement>
          <dateTimeValue>10/16/2006</dateTimeValue>
          <integerValue>1</integerValue>
        </nestedElement>
      </MyConfiguration>
    </configuration>
    

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

    <configuration>
      <configSections>
        <section name="MyConfiguration" 
                 type="MyConfigurationSection, MyConfigurationAssembly" />
      </configSections>
    
      <MyConfiguration configSource="MyConfiguration.config" />
    </configuration>
    

    and your "MyConfiguration.config" would then contain:

      <MyConfiguration> 
        <nestedElement>
          <dateTimeValue>10/16/2006</dateTimeValue>
          <integerValue>1</integerValue>
        </nestedElement>
      </MyConfiguration>
    

    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

    0 讨论(0)
  • 2020-11-27 15:38

    Here's the "Add existing item" dialog in VS 2008:

    Add existing item dialog

    Click on the little dropdown indicator on the "Add" button and pick "Add as Link" from the context menu.

    Marc

    0 讨论(0)
  • 2020-11-27 15:40

    One design option is to avoid accessing the app.config directly from your class library projects altogether, thus avoiding the extra external dependency.

    Rather, only your executable project knows about the config file and it can explicitly pass the appropriate config information to the libraries when it creates objects from them or initializes them.

    0 讨论(0)
提交回复
热议问题