我想更改写在 app.config 文件中的应用程序的连接字符串。我喜欢直接从我的应用程序中更改连接字符串。
我的 app.config 看起来像:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
...
<connectionStrings>
<add name="ApplicationName.My.MySettings.ConnectionS"
connectionString="Data Source=xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="Test" value="/" />
</appSettings>
</configuration>
我可以使用以下代码在appSettings中进行更改:
Dim config As System.Configuration.Configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
config.AppSettings.Settings("Test").Value = "New value"
但是在安装应用程序时,我无法在调试时对connectionStrings进行更改:
Dim config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim connectionStringsSection = DirectCast(config.GetSection("connectionStrings"), ConnectionStringsSection)
connectionStringsSection.ConnectionStrings("ApplicationName.My.MySettings.ConnectionS").ConnectionString = "New connection string"
config.Save()
ConfigurationManager.RefreshSection("connectionStrings")