I’m attempting to update some values in the web.config file from code during an install process.
So far I’ve found this for updating the connection string,
' Open Application's Web.Config
Dim config = WebConfigurationManager.OpenWebConfiguration("/" + TargetVDir, friendlySiteName)
'Add new connection string setting for web.config
Dim appDatabase = New ConnectionStringSettings()
appDatabase.Name = "TimeOffEntities"
appDatabase.ConnectionString = EFconnectionstring
config.ConnectionStrings.ConnectionStrings.Clear()
config.ConnectionStrings.ConnectionStrings.Add(appDatabase)
' Persist web.config settings
config.Save()
However I need to update another section and I’m not sure how. I have the settings for an email and I’m not sure how to update them. Relevant web.config section below,
<configuration>
<system.net>
<mailSettings>
<smtp>
<network
host="relayServerHostname"
port="portNumber"
userName="username"
password="password" />
</smtp>
</mailSettings>
</system.net>
</configuration>
You can do it as follows:
And of course similarly for other configuration sections.
If you click on “More…” in the Inheritance Hierarchy section of the MSDN documentation for the ConfigurationSection class you’ll get a list of the ConfigurationSection-derived types for all the standard configuration sections.