title: How to use .NET configuration files (app.config, settings.settings) to save and restore all application data?
body: <p>Although there are a lot of posts about .net config files, I believe that my requirements do not allow for any of the solutions proposed (or I don't understand the process enough to make it work for me).</p>
  
  <p>The situation is that I have a Windows Forms application (could just as well be another app) which binds certain user input fields (e.g. IP Address), as well as form properties (window size, etc) to application settings (in the Properties->Settings area). From what I understand, these settings are the same as those represented in my project's app.config file, so I should be able to use the System.Configuration.ConfigurationManager class to manipulate those settings.</p>
  
  <p>What I want to do is to allow the user to export and import all of the saved settings. Rather than doing serialization of some custom object or working with INI files, I thought it would be easier to just save and replace the config file that the application uses.</p>
  
  <p>It is relatively easy to save the current settings to a specified file:</p>
  
  <pre><code>internal static void Export(string settingsFilePath)
  {
      var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
      config.SaveAs(settingsFilePath);
  }
  </code></pre>
  
  <p>However, restoring a config file to overwrite the current settings is proving difficult. <strike>It seems like I can open a configuration file like so</p>
  
  <pre><code>var newConfig = ConfigurationManager.OpenExeConfiguration(settingsFilePath)
  </code></pre>
  
  <p>but I don't see how to completely replace all of the settings in the current config with those of the imported file.</strike> [Edit: This overload is supposed to receive the path of the .exe, not the .config file. Opening an exe file in this manner will possibly throw a ConfigurationErrorsException if the calling code doesn't reference the same assemblies in the config file.]</p>
  
  <p>Perhaps I only need to use the methods outlined in <a href="https://stackoverflow.com/questions/132544/net-configuration-app-config-web-config-settings-settings">other posts</a> to replace only a section of the config, and not the whole thing, but I don't see how that would work at this point.</p>
  
  <p>Any ideas? Am I going down the right track, or should I just use INI files (or something else)?</p>