ehh
ehh

Reputation: 3480

How to add a dll config file to a project that add the dll as reference?

I wrote a UserLogin dll. The dll is using App.Config file with some default parameters. I have renamed the config file to UserLogin.Config for better files system readability.

I wrote a new application requiring login of users. I added the UserLogin.dll as reference to the new application. The problem is that the config file was not added with the reference. I have to add it manually to the bin\debug folder to be able to run the application through Visual Studio.

I do not have a such problem for deploying it since I am adding the config file as part of the setup.

What is the correct way to add the config file to the new application so I can run it in development environment?

Following is the UserLogin.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="IsTestEnvironment" value="false" />
        <add key="AllowedUserForTestEnvironment" value="ehh" />
        <add key="EnableLogging" value="false" />
     </appSettings>
</configuration>

The class that reads from the UserLogin.config file:

public static class ConfigurationReader
{
    private static AppSettingsSection _appSettings;

    public static AppSettingsSection AppSettings
    {
        get
        {
            if (_appSettings == null)
            {
                var userPreferencesConfigFileMap = new ExeConfigurationFileMap();
                userPreferencesConfigFileMap.ExeConfigFilename = "UserLogin.config";
                var userPreferencesConfig = ConfigurationManager.OpenMappedExeConfiguration(userPreferencesConfigFileMap, ConfigurationUserLevel.None);
                _appSettings = (AppSettingsSection)userPreferencesConfig.GetSection("appSettings");                                        
            }

            return _appSettings;
        }
    }

    public static bool IsTestEnvironment
    {
        get
        {
            return bool.Parse(AppSettings.Settings["IsTestEnvironment"].Value);
        }
    }

    public static string AllowedUserForTestEnvironment
    {
        get
        {
            return AppSettings.Settings["AllowedUserForTestEnvironment"].Value;
        }
    }

    public static string EnableLogging
    {
        get
        {
            return AppSettings.Settings["EnableLogging"].Value;
        }
    }
}

The new Application that use the UserLogin.dll:

enter image description here

Upvotes: 2

Views: 3274

Answers (1)

NatarajC
NatarajC

Reputation: 123

The problem is you have renamed the config file name to a custom name.

Ideally any DLL can use configuration from the parents Exes App.config, and you only need to add your appsettings to the App.config file of the application(exe) which is using the dll.

Why did you separate it this way. This is not a good practice.

Now since you separated it, it is much like same as having a custom xml configuration file.

In such case you can try to add Post-Build or Pre-Build command using XCOPY to copy the config file to the right output directory so that DLL picks it up.

Right click project properties, go to Pre-Build event, add a command like

xcopy /y "$(SolutionDir)MyProject\myFile.xxx" "$(OutputDir)"

Note you may need to change the Path and file name to suit your need.

Upvotes: 4

Related Questions