编辑 Web.Config 文件
我试图在安装过程中通过代码更新 web.config 文件中的某些值。
到目前为止,我已经找到了用于更新连接字符串的方法,
' Open Application's Web.Config
Dim config = WebConfigurationManager.OpenWebConfiguration("/" + TargetVDir, friendlySiteName)
'Add new connection string setting for web.config
Dim appDatabase = New ConnectionStringSettings()
appDatabase.Name = "TimeOffEntities"
appDatabase.ConnectionString = EFconnectionstring
config.ConnectionStrings.ConnectionStrings.Clear()
config.ConnectionStrings.ConnectionStrings.Add(appDatabase)
' Persist web.config settings
config.Save()
但是我需要更新另一个部分,但我不确定如何更新。我有电子邮件设置,但不知道如何更新它们。下面相关的 web.config 部分,
<configuration>
<system.net>
<mailSettings>
<smtp>
<network
host="relayServerHostname"
port="portNumber"
userName="username"
password="password" />
</smtp>
</mailSettings>
</system.net>
</configuration>
I'm attempting to update some values in the web.config file from code during an install process.
So far I've found this for updating the connection string,
' Open Application's Web.Config
Dim config = WebConfigurationManager.OpenWebConfiguration("/" + TargetVDir, friendlySiteName)
'Add new connection string setting for web.config
Dim appDatabase = New ConnectionStringSettings()
appDatabase.Name = "TimeOffEntities"
appDatabase.ConnectionString = EFconnectionstring
config.ConnectionStrings.ConnectionStrings.Clear()
config.ConnectionStrings.ConnectionStrings.Add(appDatabase)
' Persist web.config settings
config.Save()
However I need to update another section and I'm not sure how. I have the settings for an email and I'm not sure how to update them. Relevant web.config section below,
<configuration>
<system.net>
<mailSettings>
<smtp>
<network
host="relayServerHostname"
port="portNumber"
userName="username"
password="password" />
</smtp>
</mailSettings>
</system.net>
</configuration>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要推出一些您自己的 XML 解析。或者更好的是,如果您使用 .NET 4,请使用配置文件转换。
You'll need to roll out some XML parsing of your own. Or better yet, if you're on .NET 4 use config file transforms.
您可以按如下方式进行操作:
当然其他配置部分也类似。
如果单击 ConfigurationSection 类的 MSDN 文档,您将获得所有标准配置节的 ConfigurationSection 派生类型的列表。
You can do it as follows:
And of course similarly for other configuration sections.
If you click on "More..." in the Inheritance Hierarchy section of the MSDN documentation for the ConfigurationSection class you'll get a list of the ConfigurationSection-derived types for all the standard configuration sections.