Reading and Writing from Web.config
Hi Everyone. What is the simplest way to read from my web.config and write back to it? I want to store my SQL datasource there and return it when I want to open a connection to my SQL db's.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kapil Deo MalhotraPosted Apr 14, 2009, 5:43 PM
To read the AppSettings from the Web.Config file you can use the ConfigurationSettings or ConfigurationManager Class.
It works like the following
string connectionInfo = ConfigurationSettings.AppSettings["ConnectionInfo"];
ConnectionInfo is in the Web.config file under the AppSettings like
For Writting into Web.config
Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
'Modify Existeing Key in Web.config
config.AppSettings.Settings.Item("SiteName").Value = "Value Changed"config.Save(ConfigurationSaveMode.Modified)
Hope that will help.
Space GhostPosted Apr 14, 2009, 6:09 PM