I have a small class that holds two strings as follows:
public class ReportType
{
private string displayName;
public string DisplayNa
@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:
How about creating a static method which returns an instance of ReportType containing data from the config file. It's simpler and I don't think serializing is necessary.
public class ReportType
{
public static ReportType GetDefaultSelectedReportType()
{
string displayName = ConfigurationManager.AppSettings["DefaultDisplayName"];
string reportName = ConfigurationManager.AppSettings["DefaultReportName"];
return new ReportType(displayName, reportName);
}
.
.
.
}
Just a bit more clear code then Charlie's
public class ReportType
{
public static ReportType CreateDefaults()
{
return new ReportType
{
DisplayName = ConfigurationManager.AppSettings["DefaultDisplayName"],
ReportName = ConfigurationManager.AppSettings["DefaultReportName"]
};
}
}
OK I think that I have eventually worked it out. The first thing to do is to add the following attributes to each property of the ReportType class that needs to be serialised and inherit the class from ApplicationSettingsBase:
public class ReportType : ApplicationSettingsBase
{
private string displayName;
[UserScopedSetting()]
[SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]
public string DisplayName
{
get { return displayName; }
}
..............
and then, once you have rebuilt your assembly (important!) you can go into the settings screen and click browse and then type your namespace and class name in the text box at the bottom (e.g. Label_Creator.ReportType). The namespace and class name do not appear in the tree and so this part is not exactly obvious what you need to do which is why it is a bit confusing....