Archive

Archive for the ‘Configuration’ Category

Writing Values to App.config File

November 7, 2010 Leave a comment

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: ,