How to use .NET configuration files (app.config, settings.settings) to save and restore all application data?

后端 未结 5 1223
灰色年华
灰色年华 2020-12-15 12:24

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

5条回答
  •  情歌与酒
    2020-12-15 13:09

    Thanks to Manu's suggestion to just read the file as XML, I hacked together this solution. (It only works in the current form for properties saved as strings, such as the Text property of a TextBox. It won't work, for instance, if you persist the Value property of a NumericUpDown control.) It works by using Export with a path to the file to save, which produces a file like the following:

    
    
        
            
                
                    testfpga
                
                
                    test
                
            
        
    
    

    Then you Import the file and all of the settings are changed in the app (don't forget to .Save() at some point). If something goes wrong, the settings will revert back.

    using System;
    using System.Configuration;
    using System.IO;
    using System.Linq;
    using System.Xml.Linq;
    using System.Xml.XPath;
    using AedUtils;
    
    namespace HawkConfigGUI
    {
        public static class SettingsIO
        {
            private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
    
            internal static void Import(string settingsFilePath)
            {
                if (!File.Exists(settingsFilePath))
                {
                    throw new FileNotFoundException();
                }
    
                var appSettings = Properties.Settings.Default;
                try
                {
                    // Open settings file as XML
                    var import = XDocument.Load(settingsFilePath);
                    // Get the  elements
                    var settings = import.XPathSelectElements("//setting");
                    foreach (var setting in settings)
                    {
                        string name = setting.Attribute("name").Value;
                        string value = setting.XPathSelectElement("value").FirstNode.ToString();
    
                        try
                        {
                            appSettings[name] = value; // throws SettingsPropertyNotFoundException
                        }
                        catch (SettingsPropertyNotFoundException spnfe)
                        {
                            _logger.WarnException("An imported setting ({0}) did not match an existing setting.".FormatString(name), spnfe);
                        }
                        catch (SettingsPropertyWrongTypeException typeException)
                        {
                            _logger.WarnException(string.Empty, typeException);
                        }
                    }
                }
                catch (Exception exc)
                {
                    _logger.ErrorException("Could not import settings.", exc);
                    appSettings.Reload(); // from last set saved, not defaults
                }
            }
    
            internal static void Export(string settingsFilePath)
            {
                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
                config.SaveAs(settingsFilePath);
            }
        }
    }
    

提交回复
热议问题