roncli.com blog
The blog of roncli
roncli.com blog
roncli.com
blog
Profile
roncli
Houston, Texas, United States
Labels
Coding
CTG Music
Editorials
Games
Miscellaneous
Music
Servers
Silliness
Software
Sports
Trax in Space Beta
Weather
Recent Posts
New Zepsi Industries
Music
TiS Beta updates
More TiS Beta News
Starting Over
Gaming the System
What to do
More on the TiS Beta
Well, that didn't take long
#10*
Friday, July 20, 2007
Changing app.config on the fly
Posted: 1:35:00 PM 0 comments
After banging my head against the wall several times trying to insert a dynamic ConnectionString into my app.config, I figured it out. It's nowhere near obvious, though. Import System.Configuration to get it functional. Here's an example of how to modify the connectionStrings section.

' Load configuration
Dim cfgConfig As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

' Gotta try to remove it first, if it exists you'll crash out.
Try
    cfgConfig.ConnectionStrings.ConnectionStrings.Remove("conn")
Catch ex As Exception
End Try

' Add new connection
cfgConfig.ConnectionStrings.ConnectionStrings.Add(new ConnectionString("conn", strConnString, strProvider)

' Save the file
cfgConfig.Save()

' Force the application to refresh this section from the file at runtime.
ConfigurationManager.RefreshSection("connectionStrings")

Labels: , ,




0 Comments

Post a Comment