Silver Bullet Silver Bullet -5 years ago 188 Question

How can i change my app.config file's connection string at runtime?

I created my project to .exe file.
During installation on another machine, one can change the location of installing package to any drive.
In my project, i have set my app.config to point the database that is available in c:\project.
If i suppose while installing, when i change the location of installation to d:\ or anywhere, i was getting invalid access db.
What i want is, i have to reconfigure my app.config file automatically, by detecting its current db location.

I have no clue about how to do this..
Please i need your precious help doing this possible.

Answer Source
Imports System.Configuration
Imports System.Configuration.ConfigurationManager

        Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)          
        config.ConnectionStrings.ConnectionStrings("MyProject.Properties.Settings.MyProjectConString").ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=|DataDirectory|\SampleDB;Persist Security Info=True;"

Where MyProject.Properties.Settings.MyProjectConString if the name of your Project and connection string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download