Search code examples
c#.netdllapp-config

App.config with dll returns null


I have a dll I'm calling from another app. I need to store dll settings in separate xml file. So I'v created App.config file. It is looks like:

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

Than I try to get value from config with thic code:

var val=System.Configuration.ConfigurationSettings.AppSettings["myKey"];

But it always return null. Also key count of AppSettings equals zero, so there is no keys in it.

What's wrong with it? How can I store my settings in separate file?


Solution

  • If I'm not mistaken, you're looking for ConfigurationManager.OpenMappedExeConfiguration Method (ExeConfigurationFileMap, ConfigurationUserLevel).

    You need to open that assembly configuration file as an another Configuration object, and once you've got it, you'll be able to access those AppSettings.