如何在运行时更改app.config文件中的connectionString? [英] How can I change my connectionString in app.config file at runtime?
问题描述
我将我的vb.net项目创建为.exe文件。
在另一台计算机上安装期间,可以将软件包的安装位置更改为任何驱动器。
在我的项目中,我已将 app.config 设置为指向 c:\project 中可用的数据库。
如果我想在安装时将安装位置更改为** d:**或其他任何位置,则会得到个无效的访问数据库
。
我想要的是:
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 installation, when I change the location of installation to **d:** or anywhere, I get invalid access db
.
What I want is:
我想通过检测文件的当前数据库位置来自动重新配置 app.config 文件。 / p>
I want to reconfigure my app.config file automatically, by detecting its current db location.
推荐答案
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)
其中 MyProject.Properties.Settings.MyProjectConString
是项目的名称和连接字符串。
Where MyProject.Properties.Settings.MyProjectConString
is the name of your project and connection string.
这篇关于如何在运行时更改app.config文件中的connectionString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!