VB.net
- Imports System.Xml
- Imports System.Collections.Specialized
- Public Class Config
- Private m_Content As New XmlDocument()
- Private m_ConfigFile As String
- Public Sub New()
- Me.m_ConfigFile = Application.ExecutablePath & ".config"
- Me.Load()
- End Sub
- Public Sub Load()
- Try
- Me.m_Content.Load(Me.m_ConfigFile)
- Catch ex As Exception
- Logger.WriteLine(String.Format("Load config failed(GetAppConfig). Message: {0}"))
- Throw ex
- End Try
- End Sub
- Public Sub Save()
- Try
- Me.m_Content.Save(Me.m_ConfigFile)
- Catch ex As Exception
- ER.Logger.WriteLine(String.Format("Save config failed(SetAppConfig). Message: {0}"))
- Throw ex
- End Try
- End Sub
- Public Sub Refresh()
- Me.m_Content = New XmlDocument()
- Me.Load()
- End Sub
- Public Function GetConfig(ByVal key As String) As String
- Dim value As String = Nothing
- Dim element As XmlElement = Me.m_Content.SelectSingleNode(String.Format("//appSettings/add[@key='{0}']", key))
- If element IsNot Nothing Then
- value = element.GetAttribute("value")
- End If
- Return value
- End Function
- Public Sub SetConfig(ByVal key As String, ByVal value As String)
- Dim appSettings As XmlElement = Me.m_Content.SelectSingleNode("//appSettings")
- Dim element As XmlElement = appSettings.SelectSingleNode(String.Format("//add[@key='{0}']", key))
- If element Is Nothing Then
- element = Me.m_Content.CreateElement("add")
- element.SetAttribute("key", key)
- element.SetAttribute("value", value)
- appSettings.AppendChild(element)
- Else
- element.RemoveAttribute("value")
- element.SetAttribute("value", value)
- End If
- End Sub
- Public Sub RemoveConfig(ByVal key As String)
- Dim element As XmlElement = Me.m_Content.SelectSingleNode(String.Format("//appSettings/add[@key='{0}']", key))
- If element IsNot Nothing Then
- element.ParentNode.RemoveChild(element)
- End If
- End Sub
- Public Shared Function GetAppConfig(ByVal key As String) As String
- Dim configFile As String = Application.ExecutablePath & ".config"
- Dim value As String = Nothing
- Dim xmldoc As New System.Xml.XmlDocument()
- Try
- xmldoc.Load(configFile)
- Catch ex As Exception
- ER.Logger.WriteLine(String.Format("Load config failed(GetAppConfig). Message: {0}"))
- Throw ex
- End Try
- Dim element As System.Xml.XmlElement = xmldoc.SelectSingleNode(String.Format("//appSettings/add[@key='{0}']", key))
- If element IsNot Nothing Then
- value = element.GetAttribute("value")
- End If
- Return value
- End Function
- Public Shared Sub SetAppConfig(ByVal key As String, ByVal value As String)
- Dim configFile As String = Application.ExecutablePath & ".config"
- Dim xmldoc As New System.Xml.XmlDocument()
- Try
- xmldoc.Load(configFile)
- Catch ex As Exception
- ER.Logger.WriteLine(String.Format("Load config failed(SetAppConfig). Message: {0}"))
- Throw ex
- End Try
- Dim appSettings As System.Xml.XmlElement = xmldoc.SelectSingleNode("//appSettings")
- Dim element As System.Xml.XmlElement = appSettings.SelectSingleNode(String.Format("//add[@key='{0}']", key))
- If element Is Nothing Then
- element = xmldoc.CreateElement("add")
- element.SetAttribute("key", key)
- element.SetAttribute("value", value)
- appSettings.AppendChild(element)
- Else
- element.RemoveAttribute("value")
- element.SetAttribute("value", value)
- End If
- Try
- xmldoc.Save(configFile)
- Catch ex As Exception
- ER.Logger.WriteLine(String.Format("Save config failed(SetAppConfig). Message: {0}"))
- End Try
- End Sub
- Public Shared Sub RemoveAppConfig(ByVal key As String)
- Dim configFile As String = Application.ExecutablePath & ".config"
- Dim xmldoc As New System.Xml.XmlDocument()
- Try
- xmldoc.Load(configFile)
- Catch ex As Exception
- ER.Logger.WriteLine(String.Format("Load config failed(RemoveConfig). Message: {0}"))
- Throw ex
- End Try
- Dim element As System.Xml.XmlElement = xmldoc.SelectSingleNode(String.Format("//appSettings/add[@key='{0}']", key))
- If element IsNot Nothing Then
- element.ParentNode.RemoveChild(element)
- End If
- Try
- xmldoc.Save(configFile)
- Catch ex As Exception
- ER.Logger.WriteLine(String.Format("Save config failed(RemoveConfig). Message: {0}"))
- End Try
- End Sub
- End Class
沒有留言:
張貼留言