0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
krieg
  • 259
  • 1
  • 3
  • 19
  • Is it app.config or web.config file? – Pankaj Kapare Feb 03 '15 at 23:25
  • Shows up as App.config in the Solution Explorer. – krieg Feb 03 '15 at 23:25
  • possible duplicate of [Changing App.config at Runtime](http://stackoverflow.com/questions/2008800/changing-app-config-at-runtime) – MethodMan Feb 03 '15 at 23:31
  • you can also do this using `XPATH` I would suggest doing a `stackoverflow google search` there are plenty examples on how to do this – MethodMan Feb 03 '15 at 23:31
  • I did do a somewhat big google search on this, I even tried it repeatedly on several other projects to make sure it worked, when I implemented it in the webservice it didn't work which is why I ended up posting here. – krieg Feb 03 '15 at 23:32
  • 1
    If its WCF Service as question title says then it has to be web.config. If its Windows executable then it has to be app.config. Your code is for modifying contents of config file of executable and not for configuration of web application. Let us know if its exe or wcf service? – Pankaj Kapare Feb 03 '15 at 23:33
  • perhaps you are trying to update the wrong .config file.. – MethodMan Feb 03 '15 at 23:34
  • That could be possible, yet still there are only two .config files in the webservice's solution: App.config and packages.config. Considering that there's a package.config on the other projects I tested it on I am assuming that it should have worked. These are the files in the WebService's project: http://puu.sh/frftV/a0d4b6b7ed.png – krieg Feb 03 '15 at 23:35
  • 1
    I know this doesn't answer your question, but the app.settings is not the right place to do this. App settings are really for settings which are set at install time. In most situations you won't have write access to the file. Instead I'd recommend you store this information elsewhere. For example make an object which represents your "transient data" including a property for SyncDate - serialize this using something like xml or json and store that on disk. – Daniel James Bryars Feb 03 '15 at 23:42
  • You are probably right, I will do that. Thank you. – krieg Feb 03 '15 at 23:44

1 Answers1

0

You should be using WebConfigurationManager not ConfigurationManager to open the AppSettings. Give it a try.

  • For some reason there's no System.Web.Configuration reference and therefore I can't use WebConfigurationManager! – krieg Feb 04 '15 at 01:43