skip to Main Content

I have a class library project in which I want to load configuration settings.

Here is the app.config file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="editMode" value="2" />
    <add key="fileType" value="0" />
    <add key="Editor" value="C:Program FilesAdobeAdobe Photoshop CC 2015Photoshop.exe" />
  </appSettings>
</configuration>

Here is the code inside of the main program.

string savedEditor = ConfigurationManager.AppSettings["Editor"];
MessageBox.Show(savedEditor);
string savededitMode = ConfigurationManager.AppSettings["editMode"];
MessageBox.Show(savededitMode);                
string savedfileType = ConfigurationManager.AppSettings["fileType"];
MessageBox.Show(savedfileType);

It compiles OK and the *.dll.config file is created. However, it returns null for the values of the three keys. What am I doing wrong? Thanks.

2

Answers


  1. I’m just putting my comment as an answer since it’s most likely the source of problem.

    While your class library project (the thing which builds the dll) may output a config, it is not what is used when importing that class in other projects. The app.config which is used is the one which correlates to the app which is being invoked. Since you most likely do not have these values in that file, attempts to retrieve them from the AppSettings dictionary return null. To resolve the problem either copy the items below into your main applications appSettings section of it’s app.config or create an app.config with those settings if none exists.

    <add key="editMode" value="2" />
    <add key="fileType" value="0" />
    <add key="Editor" value="C:Program FilesAdobeAdobe Photoshop CC 2015Photoshop.exe" />
    
    Login or Signup to reply.
  2. Like the answer above said, the config file that will be used is for the executable calling / referencing the dll.

    So if I have an executable called “MyProgram.exe” and your dll is named “MyFirst.dll”, then “MyProgram.exe”, “MyFirst.dll” and “MyProgram.exe.config” have to be in the same directory. The “MyProgram.exe.config” file needs to have the configuration values that you’re trying to retrieve from the config file.

    Does that make sense?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search