Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read app.config from another assembly?

Tags:

c#

app-config

I have two projects:

  • Console project (Test.exe)
  • Class Library project (Test.Data.dll)

My Class Library project contains an app.config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="TestEntities" connectionString="metadata=res://*/DBNews.csdl|res://*/DBNews.ssdl|res://*/DBNews.msl;provider=System.Data.SqlClient;provider connection string=&quot;{0}&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

From the Console project I want to access the settings from the Class Library, so I've tried:

var config = ConfigurationManager.OpenExeConfiguration("Test.Data.dll");
config.ConnectionStrings.ConnectionStrings[0].Name; // LocalSqlServer
// seems to be the wrong assembly.

And:

var config = ConfigurationManager.OpenExeConfiguration("Test.Data.dll.config");
// invalid exePath

How can I access the DLL's app.config ?

like image 367
BrunoLM Avatar asked Feb 03 '11 17:02

BrunoLM


People also ask

How to get app setting value in c#?

To retrieve a value for a specified key from the <appSettings> section of the configuration file, use the Get method of the AppSettings property of the ConfigurationManager class. The ConfigurationManager class is in the System. Configuration namespace. When the AppSettings.

How read app config file in VB NET?

You can mark the settings as public in the C# project ("Access Modifier" in the property pane of settings) and then you can access it from the vb project (don't forget to add the reference).

Can we have multiple app config files?

You cannot use multiple configuration files (i.e. one per library project) without coding.


1 Answers

The DLL doesn't have its own app.config at runtime. The app.config is only there for the Entity Framework designer.

During execution, the DLL will try to read the values from the Application's app.config file. For Entity Framework connections, that means you have to copy the connection information into the Application's app.config.

like image 136
Justin Niessner Avatar answered Oct 05 '22 21:10

Justin Niessner