Solution with different project and shared configSections

204 views Asked by At

I have a solution with three projects.

  • Project1 (Windows Application)
  • Project2 (Windows Application)
  • Project3 (Class Library)

Project1 and Project2 have their own app.config files with appSettings. Now I want the add some appSettings in the Project3. But I don't want the write the new settings in both app.config files. I want a new configfile/section for my shared solution. How can I create my own config file in Project3 and reference from Project1 and Project2?

2

There are 2 answers

0
wydy On

Here is a working example. It's not perfect, but it's working:

  1. Create a share.config in Project3
<?xml version="1.0" encoding="utf-8" ?>
<shareConfig
  config1="value1">
</shareConfig>
  1. Create a ShareConfig.cs file in your Project3
public class ShareConfig : ConfigurationSection
{
    [ConfigurationProperty("config1", IsRequired = true)]
    public string Config1
    {
        get { return (string)base["config1"]; }
        set { base["config1"] = value; }
    }
}
  1. Add the config File as a Link the Project1 and Project2

  2. Add the following code in your app.config (Project1 and Project2):

<configSections>
       <section name="shareconfig" type="[classname],  [projectname], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    </configSections>
    <shareconfig configSource="App.Share.config"/>
  1. Now you can use your new config Section like this:
ShareConfig config = (ShareConfig )ConfigurationManager.GetSection("shareConfig ");
2
Chui Tey On

Have you tried:

<appSettings configSource="shared.config">
</appSettings>

Then in shared.config

<?xml version="1.0"?>
<appSettings>
  <add key="foo" value="bar" />
</appSettings>