要编写,按此方法调用:
昏暗配置作为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