0

I have moved App.config file which contains some AppSettings into DLL project from the EXE project of same solution. After that I have noticed using Configuration manager, I get only null values.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
    <add key="Path1" value="C:\ProgramData\Resources\file2.xml" />
    <add key="Path2" value="C:\ProgramData\Development\file1.xml" />
</appSettings>
</configuration>

/* Reading */
public string Path1=> ConfigurationManager.AppSettings["Path1"];
public string Path2=> ConfigurationManager.AppSettings["Path2"];

If I move back the App.config into my EXE project, I'm able to read all values right using configuration manager.

So, my question is if I have only DLL (COM) project in my solution where I dont have control on EXE(developed by 3rd party) project then how to manage the settings using App.config file?

RJN
  • 696
  • 8
  • 17
  • So is the exe referencing the dll, and you're trying to have the dll read the config? – Broots Waymb Jul 19 '18 at 16:03
  • Yes. But in reality, I don't have control on EXE as it is created by someone else and we are DLL provider. In this case to maintain settings, I don't have any other way other than having App.config with DLL – RJN Jul 19 '18 at 18:08

2 Answers2

3

Config files do not get compiled into the dll.

The app will use the app.config from the startup project of your solution unless you put in special code to look elsewhere.

This is by design so that you can change configuration with out having to recompile just because a setting changed.

Chad
  • 1,512
  • 1
  • 16
  • 40
  • I didn't see anything in the question that hinted at compiling that file in? It sounded like the file just got moved from one project to another, unless I misunderstood. – Broots Waymb Jul 19 '18 at 15:58
  • 1
    More accurately, config values are read from the config file of the startup project, not any dependencies. – stuartd Jul 19 '18 at 15:59
  • @BrootsWaymb - I read it as he removed the app.Config from his project, Maybe I am misreading too – Chad Jul 19 '18 at 16:01
  • @stuartd - and I think that is probably the key to the question. We aren't told the relationship to the exe and dll, or under what conditions the dll is trying to access the config, but if it's exe > dll > config, then that would explain it. – Broots Waymb Jul 19 '18 at 16:02
  • What if my solution doesn't have EXE project at all, as its COM DLL project consumed by some 3rd party EXE? – RJN Jul 19 '18 at 16:11
  • 1
    @RJN - [This question](https://stackoverflow.com/q/594298/715292) will probably help you understand better than I can explain in comments – Chad Jul 19 '18 at 16:18
1

You shouldn't be using app.config for any settings in class library. It's bad practice to do so and that is the reason even Microsoft doesn't provide app.config in class library.

Any value required by class library should be passed to the class from application using parameters. For instance, in your case file path can be passed as parameter to your class method or constructor etc.

But if you still want to access try this options
1. Add reference to System.Configuration.dll to your class library and add using System.Configuration in using block
2. Write code to read app.config file as normal xml file. Somewhat like this.

   string getFilePath()
   {
    string path = Path.GetDirectoryName(Assembly.GetCallingAssembly().CodeBase) + @"\ClassLibrary.dll.config";

    XDocument doc = XDocument.Load(path);

    var query = doc.Descendants("appSettings").Nodes().Cast<XElement>().Where(x => x.Attribute("key").Value.ToString() == key).FirstOrDefault();

    if (query != null)
    {
        return query.Attribute("value").Value.ToString();
    }
rk.
  • 11
  • 1
  • mine is DLL project and I don't have control on EXE, so I dont have other option to store many settings. – RJN Jul 19 '18 at 18:11
  • @RJN - Ok, got it. You can try one of the above mentioned option and see if it gets you desired result. – rk. Jul 20 '18 at 16:31
  • Thank you. This worked for me! On a side note, don't try to use ConfigurationManager. It cannot read a config file from inside a class library. For example, the following code will NOT work: string value = ConfigurationManager.ConnectionStrings[key] ; string value = ConfigurationManager.AppSettings[key]; Reading via the xml doc and system.IO is your only option. – AV2000 Oct 03 '21 at 23:27