问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/30948653/saving-user-scoped-settings-applicationsettingsbase