I have two projects:
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="{0}"" 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
?
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.
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).
You cannot use multiple configuration files (i.e. one per library project) without coding.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With