0

While debugging my C# WinForm app, out of the nowhere this exception started coming out on code that previously worked okay. In fact, the Class in which that piece of code is doesn't even get initialized at startup. I have no clue what's going on. If I uncomment that code, the exception gets thrown on another piece of code like if some other thing is wrong in the project. It looks like a corrupted file or something that's gone bad. I am in kind of a hurry for delivery time so it's driving me crazy. Here is the exception detail:

System.Configuration.ConfigurationErrorsException


HResult=0x80131902
  Message=Error en la inicialización del sistema de configuración
  Source=System.Configuration
  StackTrace:
   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
   at System.Net.Configuration.SettingsSectionInternal.get_Section()
   at System.Net.Sockets.Socket.InitializeSockets()
   at System.Net.Dns.GetHostName()
   at Sigma_Admin.Custom_Classes.SygmaServer.GetIPAddress()
   at Sigma_Admin.Custom_Classes.SygmaServer..ctor()
   at Sigma_Admin.Main..ctor()
   at Sigma_Admin.Program.Main()

Inner Exception 1:
ConfigurationErrorsException: Root element is missing.

Inner Exception 2:
XmlException: Root element is missing.

EDIT:

Here's my app.config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="Sigma_Admin.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
    </startup>
  <userSettings>
    <Sigma_Admin.Properties.Settings>
      <setting name="defaultDataFile" serializeAs="String">
        <value>null</value>
      </setting>
      <setting name="ownerAuth" serializeAs="String">
        <value>jgd</value>
      </setting>
      <setting name="employeeAuth" serializeAs="String">
        <value>ejecutivo</value>
      </setting>
      <setting name="interes" serializeAs="String">
        <value>18</value>
      </setting>
      <setting name="adminIP" serializeAs="String">
        <value>0</value>
      </setting>
    </Sigma_Admin.Properties.Settings>
  </userSettings>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="LibPowerCrypt4" publicKeyToken="43323c0f37428f68" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
  • Check your `app.config` and also `machine.config` of .NET Framework to see if they were modified and corrupt. Ideally, change to another machine, and your program should work again. – Lex Li Jul 08 '18 at 04:15

1 Answers1

1

The exception is saying that an XML file is not well-formed, by not having a tag at the top level of the XML file, wrapping the entire contents.

The stack trace indicates this is coming from ConfigurationManager, which retrieves things from the app.config file (which is an XML file) of the executable.

Edit that file in an XML editor, and look for the editor highlighting any mistakes in the formatting of the file; or find it yourself - if you understand XML, you should be able to spot it.

Failing that, the app.config file gets compiled to either a *.exe.config or a Web.config, and technically, that is the file which is faulty; so if no problem was found with the app.config, check the file that it becomes.

The top-most tag should be configuration. If you have configSections as a child element, then that must be the first child element.

Edit after app.config was added to question

I added your app.config to an empty WinForms project (and tweaked the namespace to match). Visual Studio then notified me that it had added the settings from the config file into my Properties\Settings.settings; and it worked fine for me. So that rules out the file itself.

Try what I tried above: create a new WinForms project with a completely different name, copy in your app.config and replace "Sigma_Admin" with your new project name, and see whether that works (put some simple code to retrieve a value). If that fails for you, then check the machine.config in the dot net framework folder, as LexLi suggested.

If the above works for you, then I'd suspect it's having trouble loading any existing settings - maybe the "Sigma_Admin" settings have significantly changed somehow, and don't match the ones that have previously been saved? See if you can delete old configuration files: type %LOCALAPPDATA% in Windows Explorer address bar and press return. Remove config files from the folder with your application's name. Then go to %LOCALAPPDATA%\..\Roaming and do the same. Then try to restart your application.

Edit after further information added in comment

Given that it works in the new project and not the old one, I think we can rule out machine.config.

Since you've noticed that it works in Debug and not in Release, I'm wondering whether there is a config transformation file for one config but not the other (for example), or a post build step which is modifying the file: by default, those two configs build into different folders... does the compare of those two config files highlight anything?

Also, you mentioned an installer for the first time... I'm going to ignore that as it sounds like the problem happens just by debugging from VS. Correct me if I'm wrong.

Richardissimo
  • 5,596
  • 2
  • 18
  • 36
  • Already checked both `*.exe.config` and `app.config` thoroughly and it all seems to be okay. I'm lost at the moment. – Joaquin Guevara Jul 08 '18 at 11:51
  • Just added my app.config file. It all seems to be okay. – Joaquin Guevara Jul 08 '18 at 15:11
  • The info you added didn't work. A new project did work but nothing changed on the main project. There's an important thing I just discovered, Usually I would test the app on "Release" "Any CPU" on VS. I changed Release to Debug and it worked! It seems that if I keep going this way and build the installer whenever a Release test is needed, things would just go swift again, but it's a big uncertainity why the Release option keeps getting this error. – Joaquin Guevara Jul 08 '18 at 22:41
  • @JoaquinGuevara another question – Richardissimo Jul 09 '18 at 05:46