1

I've written following code to make changes to the values of AppSettings in the web.config and when I tested it on my dev machine, it works fine. However, after I've copied the page to the production server, changing the values are not updated.

Configuration myConfig = WebConfigurationManager.OpenWebConfiguration("~");
        try
        {
            myConfig.AppSettings.Settings["username"].Value = txtUsername.Text;
            myConfig.AppSettings.Settings["password"].Value = txtPassword.Text;
            myConfig.AppSettings.Settings["senderNum"].Value = txtSMSCenter.Text;
            myConfig.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            lblStatus.Text = "Config updated at: " + DateTime.Now.ToString();
        }
        catch (ConfigurationException ex)
        {
            lblStatus.Text = ex.Message;
        }

I've allowed write access for Administrator, Network Service and IIS_IUSRS to web.config but still doesn't work.

Any advices?

Ye Myat Aung
  • 1,783
  • 11
  • 31
  • 49
  • Try to add permissions to the root folder of your application to `Everyone`. If this doesn't work, it's not a permission issue. – Mentoliptus Dec 14 '11 at 07:19
  • 1
    I think that, fundamentally, you are misusing the web.config file - a) do you realise that if you write a user's password into the web config file in plain text then anyone with access to the file system will be able to see it? b) there is only one web.config. What if multiple users are trying to use the site? c) web.config is not designed to be dynamically updated at runtime - each time it is updated the website will be recycled - it should be changed only when the you need to change the way the application runs, for all users – Adam Ralph Dec 14 '11 at 07:20
  • please post the error message. – Pankaj Kumar Dec 14 '11 at 07:20
  • @AdamRalph Yes. Thanks for the advices and I'm aware of them too. I have intention to encrypt the password later. I'm not using it for multiple login, but that username and password is to integrate with other separated system (SMS Gateway in my case). More over, that password won't be changed every now and then. – Ye Myat Aung Dec 14 '11 at 08:05
  • @PankajKumar No errors are thrown. Only the web.config isn't updated. – Ye Myat Aung Dec 14 '11 at 08:28
  • @Ye Myat Aung: in that case, consider persisting this information in a separate file. IMHO it doesn't belong in web.config, which designed to be changed once per running instance of the application (i.e. a change means an application restart). You may also want to consider storing this in a database, or at least some data store which is decoupled from your application instance. This will allow you to scale your application with load balancing, for example, with each web site instance reading/writing this value to/from the central data store. – Adam Ralph Dec 14 '11 at 11:05

1 Answers1