2

I´ve read this post from Marc Gravell showing how to read an app.config-file from a DLL. When I try to read the userSettings from within that library I get an empty list:

var settings = ConfigurationManager.OpenExeConfiguration("CodeGeneratorApp.exe").AppSettings.Settings;

The app.config-file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="CodeGenerator.CodeGeneratorApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <CodeGenerator.CodeGeneratorApp.Properties.Settings>
          <setting name="CodeExtensions" serializeAs="Xml">
            <value>
              <CodeExtensionList>
                <Extension AssemblyQualifiedName="CodeGenerator.Extensions.NamespaceTypesExtension, CodeGenerator, Culture=neutral">
                  <NamespaceTypesExtension />
                </Extension>
                ...
              </CodeExtensionList>
            </value>
          </setting>
        </CodeGenerator.CodeGeneratorApp.Properties.Settings>
    </userSettings>
</configuration>

My DLL and the application are located in the same folder and even when I add

var settings = ConfigurationManager.OpenExeConfiguration("CodeGeneratorApp.exe").AppSettings.Settings;

to a QuickWatch I see that the FilePath of the returned object is set to the applications config-file, so it was found and opened properly.

In particular what I´m trying to achieve is to de-serialize the value for CodeExtensionList but I don´t know how to even access that section within the file.

Community
  • 1
  • 1
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Is it because those are `userSettings` and you're asking for `AppSettings`? – Adam V Feb 17 '17 at 15:16
  • I don't think you can get to user-scope settings this way. I think you need [this ugly animal](https://msdn.microsoft.com/en-us/library/ms134269(v=vs.110).aspx). There is a lot to dislike about System.Configuration, "don't do this" is the only good advice I can think of. An EXE project ought to configure the class library it uses. – Hans Passant Feb 17 '17 at 15:18

2 Answers2

1

It seems you have got you have asked for. There is not appSettings section in your app.config. That is why AppSettings.Settings returns an empty list.

Edit

It seems you are trying to load custom section CodeGenerator.CodeGeneratorApp.Properties.Settings. If that is the case, the following code should do the work:

var settings = ConfigurationManager
    .OpenExeConfiguration("CodeGeneratorApp.exe")
    .GetSection("userSettings/CodeGenerator.CodeGeneratorApp.Properties.Settings")
klashar
  • 2,519
  • 2
  • 28
  • 38
0

I finally got it working with the help of klashar. As the information is located within the userSettings we need to query this particular section of course. This can be done by the following code:

var config = ConfigurationManager.OpenExeConfiguration("CodeGeneratorApp.exe");
var sectionGroup = (ClientSettingsSection) config.GetSection("userSettings/CodeGenerator.CodeGeneratorApp.Properties.Settings");

Now we can get the SettingElement within that group, in my case there´s only one so we can take the very first, query its content as Xml, write that Xml to a MemoryStream and de-serialize that stream:

var setting = (SettingElement) sectionGroup.Settings[0];

var ser = new XmlSerializer(typeof(CodeExtensionList));
MemoryStream stm = new MemoryStream(); 
StreamWriter stw = new StreamWriter(stm);
stw.Write(setting.Value.ValueXml.InnerXml);
stw.Flush();
stm.Position = 0;
var value = ser.Deserialize(stm) as CodeExtensionList;
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111