1

I am trying to get my application settings to save when a user exits a configuration form (not my mainform). It will keep the settings in memory because when I open the form again, it will have my data that I configured still there, but it will not save it to disk. This is the filepath it is saving the xml file to

C:\Users\david_000\AppData\Local[company name][project name]\1.0.0.0.

I am using [UserScopedSetting()] in my class that implements the ApplicationSettingsBase file, so it should save when I call,

Properties.Settings.Default.Save(); 

This is my class that uses ApplicationSettingsBase

public class DeviceConfiguration : ApplicationSettingsBase
{
    /// <summary>
    /// Initializes a new instance of the <see cref="DeviceConfiguration"/> class.
    /// </summary>
    public DeviceConfiguration()
        : base()
    {
        this.MasterDevices = new BindingList<Device>();
        this.SlaveDevices = new BindingList<Device>();
    }

    [UserScopedSetting()]
    [SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]
    public BindingList<Device> MasterDevices
    {
        get
        {
            return this["MasterDevices"] as BindingList<Device>;
        }

        set
        {
            this["MasterDevices"] = value;
        }
    }

    [UserScopedSetting()]
    [SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]
    public BindingList<Device> SlaveDevices
    {
        get
        {
            return this["SlaveDevices"] as BindingList<Device>;
        }

        set
        {
            this["SlaveDevices"] = value;
        }
    }
}

My BindingList contains multiple properties and that class is using the [Serializable] attribute. But when I save the xml file, all it saves is this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <[company.project].Properties.Settings>
            <setting name="IpAddressBESS" serializeAs="String">
                <value>192.168.3.254</value>
            </setting>
            <setting name="PortBESS" serializeAs="String">
                <value>504</value>
            </setting>
            <setting name="IpAddressInverter" serializeAs="String">
                <value>192.168.3.200</value>
            </setting>
            <setting name="PortInverter" serializeAs="String">
                <value>502</value>
            </setting>
            <setting name="StartDate" serializeAs="String">
                <value>04/08/2015 08:00:00</value>
            </setting>
            <setting name="EndDate" serializeAs="String">
                <value>04/08/2015 16:00:00</value>
            </setting>
            <setting name="DeviceConfig" serializeAs="Xml">
                <value />
            </setting>
        </[company.project].Properties.Settings>
    </userSettings>
</configuration>

Any advice on this would be greatly appreciated.

Jondkess
  • 13
  • 1
  • 4

1 Answers1

2

Without a good, minimal, complete code example that reliably reproduces the problem, it is impossible to say for sure what the problem is.

However, based on what you posted, it seems you may misunderstand the relationship between the Settings Designer and your custom ApplicationSettingsBase class.

In particular, Properties.Settings.Default would normally return an instance of a Designer-created class named Settings. Calling Properties.Settings.Default.Save(); will save only the values in that object, not those in some other class.

If you have a separate class DeviceConfiguration that you want saved (as you seem to in the code you posted), you need to handle that explicitly. Simply having an instance of a subclass of ApplicationSettingsBase won't do it. You need to call the Save() method on that custom subclass yourself.

See also How to: Create Application Settings on MSDN.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • So I called the save() method like you suggested on my DeviceConfig subclass, and that did save my settings to my user config file. But instead of saving it within the DeviceConfig value, it created two new settings named MasterDevices and SlaveDevices. I am wondering how to save the MasterDevices and SlaveDevices settings within my DeviceConfig settings. – Jondkess Jun 22 '15 at 15:42
  • I'm not really sure what you expected to happen. The XML you're showing in your question shows a "DeviceConfig" element in the "Settings" object. To save data that way will require an actual "DeviceConfig" property in your Settings object, because that's how those elements appear. Why have a separate `ApplicationSettingsBase` subclass, if you want your configurations saved under the "Settings" element? Why not just add those to the main "Settings" object as new properties? – Peter Duniho Jun 22 '15 at 17:41
  • Well that is what I would like to do, but the setting object asks for a value and I dont know how to give an instance of a class as a value. Is it possible to give an instance of a class as a value in the settings.settings? – Jondkess Jun 22 '15 at 18:22
  • So I was using ApplicationSettingsBase on my DeviceConfig class and it was creating a new field in my user.config file. So instead of that, I just made my DeviceConfig class which is 2 BindingLists made up of another class, to have a [Serializable] attribute with a { get; set; } on my two different lists, and when I called the default.save() it saved the configuration into the settings object where I wanted it to. – Jondkess Jun 22 '15 at 20:00
  • Thank you very much for the help – Jondkess Jun 22 '15 at 20:01