Silver Bullet Silver Bullet - 5 months ago 32
Vb.net Question

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

I created my vb.net 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.
Thanks.

Answer
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;"
        config.Save(ConfigurationSaveMode.Modified)

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