newprint
newprint

Reputation: 7136

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

Solution that I am working on is made of number of projects, one of them is start-up project(Windows Forms, .exe project) and has app.config file tied to it. At least one of the project(all other projects are .DLLs), that deals with database, will also needs to read app.config settings (to read db connection string). I want to centralize all of the application settings in one app.config file.

My questions are:

  1. From what I understood, application configuration files are created per projects, not per solution ?
  2. To access the app.config from DLLs should I use ConfigurationManager.OpenExeConfiguration(string exePath), where string exePath is the location of .exe for start-up project ?

Upvotes: 0

Views: 1004

Answers (3)

Joonas Koski
Joonas Koski

Reputation: 269

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;

Upvotes: 1

D Stanley
D Stanley

Reputation: 152556

Visual Studio creates an app.config for each project, but only to provide a place to store configuration items for that assembly (since it could be used by multiple executing assemblies). Those configuration items should be incorporated into the app.config of the executing assembly.

You can add code to pull from multiple config files, but it's cleaner just to put them all in one app.config for the executable.

Upvotes: 1

t3chb0t
t3chb0t

Reputation: 18665

You need to copy all the project specific configuration (like connections strings etc.) into the the main app.config.

Upvotes: 0

Related Questions