簡體   English   中英

編輯Web.Config文件

[英]Edit the Web.Config file

我正在嘗試在安裝過程中通過代碼更新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>

您將需要自己進行一些XML解析。 或者更好的是,如果您使用的是.NET 4,請使用config file transforms

您可以按照以下步驟進行操作:

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();

當然,其他配置部分也是如此。

如果您在MSDN文檔的 “繼承層次結構”部分中單擊ConfigurationSection類的 “更多...”,您將獲得所有標准配置部分的ConfigurationSection派生類型的列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM