C# Dll Config File

What use has the default (assembly).dll.config file for .NET-Assemblies?

The default values are built into the .dll file. You can of course still change those settings, but you do that in the program.exe config instead by referring to the assembly settings with a section in configSections/sectionGroup. The assembly settings can then be changed in the applicationSettings by creating a XML block with the same name as the section.

The section tag in the section group can simply be copied from the app.config file of your assembly project. That way the token, name, etc. will be correct. The same goes for the applicationSettings part. Just copy it from the app.config in the assembly project and into the app.config file of the program.exe project.

example program.exe.config:

<configuration>
<configSections>
... references to all dll settings ...
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="MyAssemblyNamespace.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
... more config stuff...
<applicationSettings>
... override your dll settings ...
<MyAssemblyNamespace.Properties.Settings>
<setting name="MaxUserNameLength" serializeAs="String">
<value>100</value>
</setting>
</MyAssemblyNamespace.Properties.Settings>
</applicationSettings>

How to have DLL specific config file?

Yes, it is possible. Just like you can have exe.config file, its perfectly normal to have a dll.config file. Store DLL specific information in its config file. Later on you can access this configuration information from DLL by following code

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string dllConfigData = appConfig.AppSettings.Settings["dllConfigData"].Value;

How can I load a dll-config using ConfigurationManager?

When you retrieve a Configuration object using the "normal" method, the object you get back is tied to the configuration of the App Domain you are executing in, rather than the particular assembly.

See this answer for more detailed information.

But in short, ConfigurationManager.Appsettings always read config file of main application, but still you can read dll configs this way:

var config = ConfigurationManager.OpenExeConfiguration("foo.dll");

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

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.

Where to save configuration file for dll in GAC - Best Practice

Since you want the changes to be shared across all EXEs that use the DLL, you do not want to use an app.config. Every exe has it's own App.config so the settings would be remembered per-executable. Instead, use the project "Settings" option.

Right click on your DLL project and go to Properties. Then pick the "Settings" tab and click the link to create a settings file. This will create a .settings file. These default values are compiled into your application so there is nothing in the app.config files. Settings can be read and updated and are saved in the user's directory

Put your default settings in there. You can get to them by accessing YourProjectName.Settings.Default.SettingName. You can change them then call YourProjectName.Settings.Default.Save.

Equivalent to 'app.config' for a library (DLL)

You can have separate configuration file, but you'll have to read it "manually", the ConfigurationManager.AppSettings["key"] will read only the config of the running assembly.

Assuming you're using Visual Studio as your IDE, you can right click the desired project → Add → New item → Application Configuration File

Sample Image Sample Image

This will add App.config to the project folder, put your settings in there under <appSettings> section. In case you're not using Visual Studio and adding the file manually, make sure to give it such name: DllName.dll.config, otherwise the below code won't work properly.

Now to read from this file have such function:

string GetAppSetting(Configuration config, string key)
{
KeyValueConfigurationElement element = config.AppSettings.Settings[key];
if (element != null)
{
string value = element.Value;
if (!string.IsNullOrEmpty(value))
return value;
}
return string.Empty;
}

And to use it:

Configuration config = null;
string exeConfigPath = this.GetType().Assembly.Location;
try
{
config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);
}
catch (Exception ex)
{
//handle errror here.. means DLL has no sattelite configuration file.
}

if (config != null)
{
string myValue = GetAppSetting(config, "myKey");
...
}

You'll also have to add reference to System.Configuration namespace in order to have the ConfigurationManager class available.

When building the project, in addition to the DLL you'll have DllName.dll.config file as well, that's the file you have to publish with the DLL itself.

Within the VS project, you should set the .config file "Copy to output directory" setting to "Always Copy".

The above is basic sample code, for those interested in a full scale example, please refer to this other answer.

reading content of an config file into the dll associated with it

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<applicationSettings>

</applicationSettings>
<appSettings>
<add key="ReadIndex" value="C:\Index"/>
</appSettings>
</configuration>


var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var location = executingAssembly.Location; //C:\MyApp\bin\Debug\Search.dll
var config = ConfigurationManager.OpenExeConfiguration(location);
var sections = config.Sections; //count of this is 21
string s = config.AppSettings.Settings["ReadIndex"].Value.ToString();


Related Topics



Leave a reply



Submit