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

后端 未结 7 1850
小鲜肉
小鲜肉 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条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 14:57

    You need to use WebConfigurationManager.OpenWebConfiguration(): For Example:

    Dim myConfiguration As Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
    myConfiguration.ConnectionStrings.ConnectionStrings("myDatabaseName").ConnectionString = txtConnectionString.Text
    myConfiguration.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text
    myConfiguration.Save()
    

    I think you might also need to set AllowLocation in machine.config. This is a boolean value that indicates whether individual pages can be configured using the element. If the "allowLocation" is false, it cannot be configured in individual elements.

    Finally, it makes a difference if you run your application in IIS and run your test sample from Visual Studio. The ASP.NET process identity is the IIS account, ASPNET or NETWORK SERVICES (depending on IIS version).

    Might need to grant ASPNET or NETWORK SERVICES Modify access on the folder where web.config resides.

提交回复
热议问题