10

I have a console application written in C# under .net 4.0 It has a bunch of variables which I want to move into App.Config (so it will be all in one place). Added this part of code to App.Config (between configuration tags):

<configuration>
  <appSettings>
    <add key="RemoteDirectory" value="Some Text Here"/>
  </appSettings>
</configuration>

In my program trying to test it with this code

Console.WriteLine(ConfigurationManager.AppSettings["RemoteDirectory"]);

but I keep getting "Configuration system failed to initialize" error.

  • 1
    Possible duplicate of [Configuration System Failed to Initialize](https://stackoverflow.com/questions/6436157/configuration-system-failed-to-initialize) – mdiehl13 Aug 13 '17 at 02:05

9 Answers9

20

Try looking the Inner Exception for more detailed information. It helped me out when I had this same trouble.

Also, check if the format is correct for the .net Framework you are using on your project. If you're using the framework 4.5 it should look like the one below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="RemoteDirectory" value="Some Text Here" />
  </appSettings>
</configuration>
Guilherme Melo
  • 494
  • 4
  • 8
11

The exception "Configuration system failed to initialize" is raised when one declares say an "appSettings" tag or any other tag after the "configuration" root tag, before declaring the "configSections" tag.

The schema of a configuration file requires the "configSections" tag to be the first child of the root tag.

<configuration>
  <configSections>
    <section name="xY" type=""/>
  </configSections>  
  <startup> 
    <supportedRuntime version="v4.0" sku=".NET Framework,Version=v4.5.2" />
  </startup>
  <xY></xY>
</configuration>
approxiblue
  • 6,982
  • 16
  • 51
  • 59
Johnny Camby
  • 578
  • 7
  • 11
1

Make sure that the configSections section is placed right after configuration section (First child)

1

I also meet with this error. In my status I mixed <configSection> and <connectionStrings> prioritise. I changed the order of the tags. First i write <configSection> then <connectionStrings> finally it fixed. Hope it will help someone.

arslanaybars
  • 1,813
  • 2
  • 24
  • 29
0

Since it is a directory, I am assuming you are using incorrect symbols there.. perhaps a /?

Abhinav
  • 2,085
  • 1
  • 18
  • 31
0

appSettings must be spelt right. It should be appSettings - S should be capital. I had it all lower case and got this error.

Rob Sedgwick
  • 4,342
  • 6
  • 50
  • 87
0

Found the issue Read the inner exception hope your code is in the try{}catch(){} block. My Inner exception reads; Only one "configSections" element allowed per config file and if present must be the first child of the root "configuration" element.

Its self explanatory Hope this helps you.

mut tony
  • 357
  • 7
  • 9
0

Good day,

I have had that same problem in a certain PC of one of our clients. I believe it is not the same kind of problem since in my case it was that the file C:\Users\"youruser"\AppData\Local\"ProgramEditorName"\"Program.exekeytoprogram"\"ProgramVersion"\user.config file got corrupted in that particular pc. I copied that file for backup just in case and deleted the file.

It worked for me I hope it can help others.

Have a nice day,

Xabier

Zitu
  • 1
0

Check for an Inner Exception, it seems to say exactly what is wrong with the config.

Gerrie Pretorius
  • 3,381
  • 2
  • 31
  • 34