Single App.Config Multi-Project C#

single app.config multi-project c#

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;

How to use app.config for multiple projects in same solution?

  1. Add New Solution Folder (menu item on the solution node of the Solution Explorer) called "Shared".
  2. Add/create an app.config file to the folder.
  3. For each project that needs using the app.config file, Add Existing Item -> navigate to the file location -> select the file -> press down-pointing triangle on the Add button of the item/file selection dialog and select Add as Link.

Where to put app.config file in solution with multiple projects- error

Usually I have one app.config per project, but real values are kept in the main (usually UI) application's config. Then I create some Core/Utility project and there I create one class that's responsible for exposing configuration keys/values. The Core/Utility project has no dependencies, but can be (and is!) referenced by any project that needs config values. It gives me a really simple layer of abstraction if one day I would like to switch to e.g. database driven configuration.

So in the end: put everything in windows forms config. It should be absolutely reachable from the class library if using ConfigurationManager class.

Edit#1:
first of all if you want to store and use connection string, do it properly, which means:
in the app.config:

<connectionStrings>
<add name="ConnString" connectionString="server..."/>
</connectionStrings>

in code:

ConfigurationManager.ConnectionStrings["ConnString"];

Try now. I'm pretty sure you're doing something wrong because your code should be working.

How to open specific .config file in multi project solution?

Config files are connected to application domains, not DLLs. You can access your appication's (let's say web application or console application) configuration directly with ConfigurationManager class (OpenExeConfiguration not required).

If you need different connection strings in different part's of the application, you can add multiple connectionstrings in your configuration

<connectionStrings>
<add name="Connection1" connectionString="Data Source=..." />
<add name="Connection2" connectionString="Data Source=..." />
</connectionStrings>

and access them by name:

var connectionstring1 = ConfigurationManager.ConnectionStrings["Connection1"].ConnectionString;
var connectionstring2 = ConfigurationManager.ConnectionStrings["Connection2"].ConnectionString;

How can a single configuration be shared between projects in a solution?

instead of adding the app.config files to each project, you could have one app.config file in your solution and link this one file to both of the projects, as described here: Share code with Add as Link.

Edit:

If you want to be able to load an external app.config, you can check follow the instructions of this post,



Related Topics



Leave a reply



Submit