App.Config errors with “Configuration system failed to initialize”

隐身守侯 提交于 2019-12-09 16:15:46

问题


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:


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>



回答2:


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>



回答3:


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




回答4:


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.




回答5:


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




回答6:


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




回答7:


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.




回答8:


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




回答9:


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



来源:https://stackoverflow.com/questions/21683473/app-config-errors-with-configuration-system-failed-to-initialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!