How do you modify the web.config appSettings at runtime?

后端 未结 7 1803
小鲜肉
小鲜肉 2020-11-22 14:28

I am confused on how to modify the web.config appSettings values at runtime. For example, I have this appSettings section:


  

        
7条回答
  •  旧时难觅i
    2020-11-22 14:49

    Try This:

    using System;
    using System.Configuration;
    using System.Web.Configuration;
    
    namespace SampleApplication.WebConfig
    {
        public partial class webConfigFile : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //Helps to open the Root level web.config file.
                Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
                //Modifying the AppKey from AppValue to AppValue1
                webConfigApp.AppSettings.Settings["ConnectionString"].Value = "ConnectionString";
                //Save the Modified settings of AppSettings.
                webConfigApp.Save();
            }
        }
    }
    

提交回复
热议问题