0

I have a DLL library Configuration in my solution and many applications call this library. This library needs the connection string to the database. Instead of storing the connection string in all applications I wanted to store it once in the DLL Configuration.
As a solution it was proposed to store it in the properties of the DLL Library Configuration. Thus, I save it there and read it with the following function:

public string ConnString()
{
    return Properties.Settings.Default.ConnectionString;
}

In the calling applications I call successfully this function:

Dim d as new Configuration.ConfigurationRepository("")
msgbox(d.ConnString())

But now I want to change the value in the value in the DLL and thus change the value in the Configuration.dll.config. But when I run the applications they get the old value of Configuration and don't update to get the new value which has been set manually. Just if I go in the project Configuration and go to Properties, then I get a message box the value has been changed and if I want to update. Then just if now I agree to update and run an application again, I get the updated value. The Configuration.dll.config looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Configuration.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup><applicationSettings>
        <Configuration.Properties.Settings>
            <setting name="ConnectionString" serializeAs="String">
                <value>werwrwerwre</value>
            </setting>
        </Configuration.Properties.Settings>
    </applicationSettings>
</configuration>

How can I get the updated connection string in the config file from the DLL Configuration?

Code Pope
  • 5,075
  • 8
  • 26
  • 68
  • 2
    code of how you're saving the new value? – blaze_125 Aug 14 '18 at 17:23
  • 1
    or, explain how you "manually" set this value? By hand? Do you restart the application or are you setting it while the application is running? – Stefan Aug 14 '18 at 17:24
  • are you calling the `Save()` method after you have set the new value? `Properties.Settings.Default.Save();` – blaze_125 Aug 14 '18 at 17:29
  • A DLL cannot have a configuration file, only the appname.exe.config file is used. Consider creating your own text or xml file that is easy to find back in a well-known location. One of the few practical uses for c:\programdata. – Hans Passant Aug 14 '18 at 17:49
  • @blaze_125, Stefan, I am doing it manually: opening the config file, changing the value and saving it. Then restarting the application. This is the required process so that later the administrator of the server is able to change manually the connection string of the DLL Library manually and after restarting the applications, the new connection string should be called – Code Pope Aug 14 '18 at 18:46

1 Answers1

2

Since a DLL can't have a configuration file, you need to read settings from a dll's configuration file 'manually'.

This means that if you have added an app.config file with name yourdll.dll.config to your class library project and the config file looks similar to this :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="ConnectionString" value="verver"/>
  </appSettings>
</configuration>

Then you need your class library to read the value in the config file in a way similar to this:

public class Config
{
    public string GetConnectionString()
    {
        Configuration config = null;
        var exeConfigPath = GetType().Assembly.Location;
        try
        {
            config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);
        }
        catch (Exception ex)
        {
            //Error handling
        }

        if (config == null) return string.Empty;

        var myValue = GetAppSetting(config, "ConnectionString");
        return myValue;
    }

    private string GetAppSetting(Configuration config, string key)
    {
        var element = config.AppSettings.Settings[key];

        if (element == null) return string.Empty;

        var value = element.Value;
        return !string.IsNullOrEmpty(value) ? value : string.Empty;
    }
}

Now you can call your class library from other projects (assuming you've added reference to the class library) and read the values from its config file in this way:

var config = new Config();
Console.WriteLine(config.GetConnectionString());

It's worth noting that you also need to set the property Copy to Output Directory of yourdll.dll.config file to Copy Always

S.Dav
  • 2,436
  • 16
  • 22
  • 1. `var exeConfigPath = GetType().Assembly.Location;` will give you the path of the calling assembly not the dll where the **yourdll.dll.config** is stored. – Code Pope Aug 15 '18 at 14:53
  • 2. This is the approach I was using until now (except that the path of the config file of the dll is either read via environment variable or is some how hard coded). But my question was how to do it via `Properties.Settings.Default.` and using the Library->Properties->Settings because in this thread (https://stackoverflow.com/questions/690313/using-app-config-with-a-class-library#answer-43064795), they claimed that it can be done with that. But it seems to me that it is a false claim. – Code Pope Aug 15 '18 at 14:53