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?
- Add New Solution Folder (menu item on the solution node of the Solution Explorer) called "Shared".
- Add/create an app.config file to the folder.
- 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 selectAdd 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
Compare Version Numbers Without Using Split Function
Is Async Await Keyword Equivalent to a Continuewith Lambda
How to Compare Values of Generic Types
How to Get the File Size in C#
How Much Memory Does a C#/.Net Object Use
What Are the Downsides to Turning Off Proxycreationenabled for Ctp5 of Ef Code First
Excel to Datatable Using Epplus - Excel Locked for Editing
Linq to Entities Only Supports Casting Edm Primitive or Enumeration Types with Ientity Interface
What Is the Algorithm to Convert an Excel Column Letter into Its Number
Why We Need Thread.Memorybarrier()
Why Is Array.Length an Int, and Not an Uint
How to Split a String by Strings and Include the Delimiters Using .Net
Automapper Convert from Multiple Sources
Wpf: How to Override Part of a Controltemplate Without Redefining the Whole Style