要編寫,按此方法調用:
昏暗配置作爲System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( 「〜」)
返回AddOrUpdateAppSetting(配置,「YourSettingKey 」,‘YourValueForTheKey’)
要閱讀,並確保你得到的值文件,而不是那些在高速緩存中,讀這樣說:
Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("~")
Return config.AppSettings.Settings("TheKeyYouWantTheValue").Value
完整的示例:
Protected Shared Function AddOrUpdateAppSetting(_
ByVal Config As System.Configuration.Configuration _
, ByVal TheKey As String _
, ByVal TheValue As String _
) As Boolean</p>
Dim retval As Boolean = True
Dim Itm As System.Configuration.KeyValueConfigurationElement = _
Config.AppSettings.Settings.Item(TheKey)
If Itm Is Nothing Then
If Config.AppSettings.Settings.IsReadOnly Then
retval = False
Else
Config.AppSettings.Settings.Add(TheKey, TheValue)
End If
Else
' config.AppSettings.Settings(thekey).Value = thevalue
If Itm.IsReadOnly Then
retval = False
Else
Itm.Value = TheValue
End If
End If
If retval Then
Try
Config.Save(ConfigurationSaveMode.Modified)
Catch ex As Exception
retval = False
End Try
End If
Return retval
End Function
那麼什麼是http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx? – 2008-10-07 16:43:57
好問題 - 我不知道:) – Seibar 2008-10-07 17:00:05
@Terrapin:請嘗試按照KieranBenton建議的RefreshSection方法,它的工作原理。在這個線程中看到我的答案:http://stackoverflow.com/questions/272097/net-dynamically-refresh-app-config – dotnetguy 2012-07-11 06:08:01