编辑 Web.Config 文件

发布于 2024-11-30 07:29:04 字数 1055 浏览 0 评论 0原文

我试图在安装过程中通过代码更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追星践月 2024-12-07 07:29:04

您需要推出一些您自己的 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.

初雪 2024-12-07 07:29:04

您可以按如下方式进行操作:

Configuration config = WebConfigurationManager.OpenWebConfiguration(...);

ConfigurationSection section = config.GetSection("system.net/mailSettings/smtp");
System.Net.Configuration.SmtpSection smtpSection = section as
                      System.Net.Configuration.SmtpSection;
if (smtpSection != null)
{
    smtpSection.Network.Host = ...;
}
config.Save();

当然其他配置部分也类似。

如果单击 ConfigurationSection 类的 MSDN 文档,您将获得所有标准配置节的 ConfigurationSection 派生类型的列表。

You can do it as follows:

Configuration config = WebConfigurationManager.OpenWebConfiguration(...);

ConfigurationSection section = config.GetSection("system.net/mailSettings/smtp");
System.Net.Configuration.SmtpSection smtpSection = section as
                      System.Net.Configuration.SmtpSection;
if (smtpSection != null)
{
    smtpSection.Network.Host = ...;
}
config.Save();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文