title: C# - User Settings broken
body: <p>We had a rare exception occur when reading the standard .Net user settings (this are the ones found in "project properties" in VS 2008):</p>
  
  <pre><code>System.Configuration.ConfigurationErrorsException was caught
    Message="Configuration system failed to initialize"
    Source="System.Configuration"
    BareMessage="Configuration system failed to initialize"
    Line=0
    StackTrace:
         at System.Configuration.ConfigurationManager.PrepareConfigSystem()
         at System.Configuration.ConfigurationManager.GetSection(String sectionName)
         at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
         at System.Diagnostics.DiagnosticsConfiguration.GetConfigSection()
         at System.Diagnostics.DiagnosticsConfiguration.Initialize()
         at System.Diagnostics.DiagnosticsConfiguration.get_IndentSize()
         at System.Diagnostics.TraceInternal.InitializeSettings()
         at System.Diagnostics.TraceInternal.get_Listeners()
    InnerException: System.Configuration.ConfigurationErrorsException
         Message="Unexpected end of file has occurred. The following elements are not closed: setting, SettingsTest.Properties.Settings, userSettings, configuration. Line 7, position 1. (C:\\Documents and Settings\\USER\\Local Settings\\Application Data\\Hitcents\\SettingsTest.vshost.exe_Url_ghwhc20utv4toanuinmj0pfsljthcugo\\1.0.0.0\\user.config line 7)"
         Source="System.Configuration"
         BareMessage="Unexpected end of file has occurred. The following elements are not closed: setting, SettingsTest.Properties.Settings, userSettings, configuration. Line 7, position 1."
         Filename="C:\\Documents and Settings\\USER\\Local Settings\\Application Data\\Hitcents\\SettingsTest.vshost.exe_Url_ghwhc20utv4toanuinmj0pfsljthcugo\\1.0.0.0\\user.config"
         Line=7
         StackTrace:
              at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
              at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
              at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
              at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(Object sender, InternalConfigEventArgs e)
         InnerException: System.Xml.XmlException
              Message="Unexpected end of file has occurred. The following elements are not closed: setting, SettingsTest.Properties.Settings, userSettings, configuration. Line 7, position 1."
              Source="System.Xml"
              LineNumber=7
              LinePosition=1
              SourceUri=""
              StackTrace:
                   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
                   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
                   at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, String res, String arg)
                   at System.Xml.XmlTextReaderImpl.ThrowUnclosedElements()
                   at System.Xml.XmlTextReaderImpl.ParseElementContent()
                   at System.Xml.XmlTextReaderImpl.Read()
                   at System.Xml.XmlTextReader.Read()
                   at System.Xml.XmlTextReaderImpl.Skip()
                   at System.Xml.XmlTextReader.Skip()
                   at System.Configuration.XmlUtil.StrictSkipToNextElement(ExceptionAction action)
                   at System.Configuration.BaseConfigurationRecord.ScanSectionsRecursive(XmlUtil xmlUtil, String parentConfigKey, Boolean inLocation, String locationSubPath, OverrideModeSetting overrideMode, Boolean skipInChildApps)
                   at System.Configuration.BaseConfigurationRecord.ScanSectionsRecursive(XmlUtil xmlUtil, String parentConfigKey, Boolean inLocation, String locationSubPath, OverrideModeSetting overrideMode, Boolean skipInChildApps)
                   at System.Configuration.BaseConfigurationRecord.ScanSections(XmlUtil xmlUtil)
                   at System.Configuration.BaseConfigurationRecord.InitConfigFromFile()
              InnerException: 
  </code></pre>
  
  <p>*NOTE: this is re-created from a test app.</p>
  
  <p>I pulled up the user.config file, and half of it was missing.</p>
  
  <p>I expect our application was terminated abruptly for some reason or another.</p>
  
  <p>This seems very rare, here is how we interact with the settings:</p>
  
  <pre><code>//How we read
  Settings settings = Settings.Default;
  _ourStaticMemberVariable = settings.OurValue;
  
  //How we save
  Settings settings = Settings.Default;
  settings.OurValue = "Our Value";
  settings.Save();
  </code></pre>
  
  <p>Is there anything wrong with how we're using it?  Both calls have a try-catch that place some default values, but the values need to be able to reset from our application.</p>
  
  <p>When in this state, our application cannot save new settings--and I cannot figure out a good way to programmatically recover.  I had to manually find the user.config and delete it.</p>
  
  <p>I also tried calling Settings.Reset(), etc. but get the same exception.</p>
  
  <p>Any ideas on how to fix this?  Or are we better off writing our own settings system or saving persistent settings in another way?</p>
  
  <p><strong>EDIT:</strong> A workaround is to delete the file from code, if you get a ConfigurationErrorsException.</p>
  
  <p>Anyone know how to get the full path of the user.config file?</p>