Home > Configuration > Writing Values to App.config File

Writing Values to App.config File


Dim tempstr As String = ""
 tempstr = Configuration.ConfigurationManager.AppSettings("dbConnection")
 Dim sqlBuilder As New SqlConnectionStringBuilder

 sqlBuilder.ConnectionString = tempstr
 sqlBuilder.InitialCatalog = SQLDatabaseName
 sqlBuilder.DataSource = SQLServerName

 ' Get the application configuration file.
 Dim config As System.Configuration.Configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(Configuration.ConfigurationUserLevel.None)

 ' Create a unique key/value pair to add to
 ' the appSettings section.
 Dim keyName As String = "dbConnection"
 Dim value As String = sqlBuilder.ConnectionString

 ' Add the key/value pair to the appSettings
 ' section.
 ' config.AppSettings.Settings.Add(keyName, value);
 Dim appSettings As System.Configuration.AppSettingsSection = config.AppSettings
 appSettings.Settings.Remove(keyName)
 appSettings.Settings.Add(keyName, value)

 ' Save the configuration file.
 config.Save(System.Configuration.ConfigurationSaveMode.Full)

 ' Force a reload in memory of the changed section.
 ' This to to read the section with the
 ' updated values.
 System.Configuration.ConfigurationManager.RefreshSection("appSettings")

Advertisements
Categories: Configuration Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: