7

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).

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.

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.

It is relatively easy to save the current settings to a specified file:

internal static void Export(string settingsFilePath)
{
    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
    config.SaveAs(settingsFilePath);
}

However, restoring a config file to overwrite the current settings is proving difficult. It seems like I can open a configuration file like so

var newConfig = ConfigurationManager.OpenExeConfiguration(settingsFilePath)

but I don't see how to completely replace all of the settings in the current config with those of the imported file. [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.]

Perhaps I only need to use the methods outlined in other posts 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.

Any ideas? Am I going down the right track, or should I just use INI files (or something else)?

Community
  • 1
  • 1
Pat
  • 16,515
  • 15
  • 95
  • 114

5 Answers5

3

Since you app.config file is a simple xml file you can load it into an XDocument:

string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
XDocument doc = XDocument.Load(path);
//Do your modifications to section x
doc.Save(path);
ConfigurationManager.RefreshSection("x");

I corrected the XDocument.Load() code according to @Pat's comment

Manu
  • 28,753
  • 28
  • 75
  • 83
  • Thanks for the recommendation, but the path you specified is not the correct one for changing user settings. Also, in case anyone else wants to try this, Load is a static method of the XDocument class, so the syntax is var doc = XDocument.Load(path);. I'll continue to pursue reading and replacing sections of the XML file to see if this method works, but no luck yet. – Pat Dec 09 '09 at 16:15
  • The path should point to your App.config where I thought the section you want to manipulate is. – Manu Dec 09 '09 at 17:45
3

It is best practice to NOT write to the app.config but use the settings file for saving user settings that are modified. The app.config and web.config should only be used for read only values.

Kenoyer130
  • 6,874
  • 9
  • 51
  • 73
  • OK, but how is that done? I am already using the settings file to persist user input between runs of the application, but how can I save and restore that information as a separate file (import/export)? – Pat Dec 08 '09 at 23:40
  • As the below post from Manu, just think of it as an XML file. – Kenoyer130 Dec 09 '09 at 03:34
  • Again, how is that done? My understanding is that the settings "file" is really just app.config, seeing as that is where the settings are stored. Clarifications? – Pat Feb 02 '10 at 23:23
  • Now that I understand this all a bit better, I know that it isn't app.config that I wanted to modify. I am using Application Settings through the Settings Designer and binding them to properties in controls on my GUI. These properties are per-user, so they are stored in user.config (which is generated when the program executes - it's not in the VS solution). – Pat Mar 05 '10 at 23:15
2

Since I did not receive any other answers and was not happy with my previous solution, I asked the question again, did some more research, and was able to come up with a better answer. See How to load a separate Application Settings file dynamically and merge with current settings? for the code and explanation.

Community
  • 1
  • 1
Pat
  • 16,515
  • 15
  • 95
  • 114
1

I do not think overriding the entire config file is a good idea. Some of the settings in this file might contain settings to be processed really early, before any of your code had a chance to do anything i.e. the ones related to the startup of .NET CLR.

mfeingold
  • 7,094
  • 4
  • 37
  • 43
  • It seems that you are right about not manipulating the entire file: In memory, at least, there are many more sections than just the "appSettings" one that I think I am interested in. To see what I mean, try var currentConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); currentConfig.Dump(); in LINQPad. – Pat Dec 08 '09 at 21:34
  • I've discovered that it isn't the "appSettings" value that I wanted to change, but the "userSettings" value. Specifically, it's the child node . – Pat Mar 05 '10 at 23:16
0

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:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <HawkConfigGUI.Properties.Settings>
            <setting name="FpgaFilePath" serializeAs="String">
                <value>testfpga</value>
            </setting>
            <setting name="FirmwareFilePath" serializeAs="String">
                <value>test</value>
            </setting>
        </HawkConfigGUI.Properties.Settings>
    </userSettings>
</configuration>

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 <setting> 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);
        }
    }
}
Pat
  • 16,515
  • 15
  • 95
  • 114
  • 1
    I would like to know why someone considers this a bad answer as well as have somebody produce a "good" answer. All I have to say is that this code *works* and there have been no other working solutions presented. – Pat Feb 02 '10 at 23:20
  • By the way, this solution *does not* manipulate the app.config file. It modifies the in-memory version of user.config which is transferred to file on application close. – Pat Mar 05 '10 at 23:18