@Calanus solution did not work for me as-is (on Visual Studio 2015).
The missing step is actually setting or getting from the actual settings.
As for the original question, implementing a simple POCO can be achieved like this:
[Serializable]
public class ReportType
{
public string DisplayName { get; set; }
public string ReportName { get; set; }
public ReportType() { }
public ReportType(string displayName, string reportName)
{
DisplayName = displayName;
ReportName = reportName;
}
}
// the class responsible for reading and writing the settings
public sealed class ReportTypeSettings : ApplicationSettingsBase
{
[UserScopedSetting]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
[DefaultSettingValue("")]
public ReportType ReportType
{
get { return (ReportType)this[nameof(ReportType)]; }
set { this[nameof(ReportType)] = value; }
}
}
I have used for actually serialization a list:
[Serializable]
public class Schedule
{
public Schedule() : this(string.Empty, DateTime.MaxValue)
{
}
public Schedule(string path, DateTime terminationTime)
{
path = driverPath;
TerminationTime = terminationTime;
}
public DateTime TerminationTime { get; set; }
public string Path { get; set; }
}
public sealed class Schedules : ApplicationSettingsBase
{
[UserScopedSetting]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
[DefaultSettingValue("")]
public List<Schedule> Entries
{
get { return (List<Schedule>)this[nameof(Entries)]; }
set { this[nameof(Entries)] = value; }
}
}
Instantiate a Schedules (ReportTypeSettings) object. It will automatically read the settings. You can use Reload method to refresh.
For instance:
ReportTypeSettings rts = new ReportTypeSettings();
rts.Reload();
rts.ReportType = new ReportType("report!", "report1");
rts.Save();
IMPORTANT NOTES:
- Note that UserScoped is intentionally used. ApplicationScoped behaves differently, so make sure you know what you are doing.
- The members are public (including the setter), and there's a default constructor even though it was needed by the code. However, serialization using XML didn't work properly. Due to time constraints I didn't investigate.
- You can change serialization to binary format as well. It will use BASE64 to store the data.
- All the settings is stored in LOCAL APP DATA folder, in a text file.