I'm trying to change the value of a key in the <appSettings>
section of the app.config
file at runtime so that it represents a proper date, but for some reason when running the program it doesn't change anything at all. I've tried the code and it worked in other occasions with test projects, but for some reason it doesn't work here.
Here's the code in question:
private void ChangeSyncDate(DateTime date)
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Open App.config
config.AppSettings.Settings["SyncDate"].Value = date.ToString();
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
And this is the function that uses that code:
public List<Business.Reservation> RequestReservationRetrieval()
{
string syncstring = ConfigurationManager.AppSettings["SyncDate"];
List<Business.Reservation> webReservations;
using (repo = new ReservationRepository())
{
webReservations = repo.ObtainReservations();
}
if(syncstring.Equals("nil"))
{
ChangeSyncDate(DateTime.Now); // and here
return webReservations;
}
else
{
DateTime syncdate = Convert.ToDateTime(syncstring);
foreach(Reservation r in webReservations)
{
if (r.Date <= DateTime.Now && r.Date >= syncdate)
webReservations.Remove(r);
}
ChangeSyncDate(DateTime.Now); // here
return webReservations;
}
}
As it stands, the value in app.config
that I mean to change isn't changing at all.
This is the value I'm trying to modify:
<appSettings>
<add key="SyncDate" value="nil"/>
</appSettings>
Thank you for taking the time to read this.