(声明:魏滔序原创,转贴请注明出处。)
第一版:http://blog.csdn.net/Modest/archive/2007/04/17/1567384.aspx
本内容是在原版的基础上做了改良,变化部分自己看看吧。
Imports System
Imports System.Xml
Imports System.Configuration
Imports System.Collections
Imports System.Reflection
Imports System.Diagnostics
Public Enum ConfigFileType
WebConfig
AppConfig
End Enum
Public Class Config
Inherits System.Configuration.AppSettingsReader
Public docName As String = String.Empty
Private node As XmlNode = Nothing
Private _configType As ConfigFileType = ConfigFileType.AppConfig
Public Property ConfigType() As ConfigFileType
Get
Return _configType
End Get
Set(ByVal value As ConfigFileType)
_configType = value
End Set
End Property
'Public Function GetConfigValue(ByVal Key As String, ByVal Type As System.Type) As Object
' Dim configurationAppSettings As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader
' Return configurationAppSettings.GetValue(Key, Type)
'End Function
'Public Function GetConfigValue(ByVal Key As String) As Object
' Dim configurationAppSettings As New System.Configuration.AppSettingsReader
' Return configurationAppSettings.GetValue(Key, GetType(String))
'End Function
Public Function GetConfigValue(ByVal Key As String) As Object
Dim cfgDoc As XmlDocument = New XmlDocument
loadConfigDoc(cfgDoc)
node = cfgDoc.SelectSingleNode("//appSettings")
If node Is Nothing Then
Throw New System.InvalidOperationException("appSettings section not found")
End If
Try
Dim addElem As XmlElement = CType(node.SelectSingleNode("//add[@key='" + Key + "']"), XmlElement)
If Not (addElem Is Nothing) Then
Return addElem.GetAttribute("value")
End If
Catch
Return Nothing
End Try
End Function
Public Function SetConfigValue(ByVal Key As String, ByVal value As String) As Boolean
Dim cfgDoc As XmlDocument = New XmlDocument
loadConfigDoc(cfgDoc)
node = cfgDoc.SelectSingleNode("//appSettings")
If node Is Nothing Then
Throw New System.InvalidOperationException("appSettings section not found")
End If
Try
Dim addElem As XmlElement = CType(node.SelectSingleNode("//add[@key='" + key + "']"), XmlElement)
If Not (addElem Is Nothing) Then
addElem.SetAttribute("value", value)
Else
Dim entry As XmlElement = cfgDoc.CreateElement("add")
entry.SetAttribute("key", key)
entry.SetAttribute("value", value)
node.AppendChild(entry)
End If
System.Windows.Forms.Application.DoEvents()
saveConfigDoc(cfgDoc, docName)
Return True
Catch
Return False
End Try
End Function
Private Sub saveConfigDoc(ByVal cfgDoc As XmlDocument, ByVal cfgDocPath As String)
Try
Dim writer As XmlTextWriter = New XmlTextWriter(cfgDocPath, Nothing)
writer.Formatting = Formatting.Indented
cfgDoc.WriteTo(writer)
writer.Flush()
writer.Close()
'cfgDoc.Save(docName)
Return
Catch
Throw
End Try
End Sub
Public Function RemoveElement(ByVal elementKey As String) As Boolean
Try
Dim cfgDoc As XmlDocument = New XmlDocument
loadConfigDoc(cfgDoc)
node = cfgDoc.SelectSingleNode("//appSettings")
If node Is Nothing Then
Throw New System.InvalidOperationException("appSettings section not found")
End If
node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"))
saveConfigDoc(cfgDoc, docName)
Return True
Catch
Return False
End Try
End Function
Private Function loadConfigDoc(ByVal cfgDoc As XmlDocument) As XmlDocument
If Convert.ToInt32(ConfigType) = Convert.ToInt32(ConfigFileType.AppConfig) Then
docName = ((Assembly.GetEntryAssembly).GetName).Name
docName += ".exe.config"
Else
docName = System.Web.HttpContext.Current.Server.MapPath("web.config")
End If
cfgDoc.Load(docName)
Return cfgDoc
End Function
End Class
Public Module ConfigModule
Private Config As New Config
Public Function GetConfigValue(ByVal Key As String) As Object
Return Config.GetConfigValue(Key)
End Function
Public Function SetConfigValue(ByVal key As String, ByVal Value As String) As Boolean
Return Config.SetConfigValue(key, Value)
End Function
Public Function RemoveElement(ByVal Key As String) As Boolean
Return Config.RemoveElement(Key)
End Function
End Module