本文发表在 rolia.net 枫下论坛Imports System
Imports System.Xml
Imports System.Configuration
Imports System.Collections
Imports System.Reflection
Imports System.Diagnostics
Public Class appConfig
Private node As XmlNode = Nothing
Public docName As String = ""
Public Function SetValue(ByVal key As String, ByVal value As String) As Boolean
Dim cfgDoc As XmlDocument = New XmlDocument()
loadConfigDoc(cfgDoc)
'retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings")
If (node Is Nothing) Then
Throw New System.InvalidOperationException("appSettings section not found")
End If
Try
'XPath select setting "add" element that contains this key
Dim addElem As XmlElement = node.SelectSingleNode("//add[@key='" + key + "']")
If (addElem.HasAttributes) Then
addElem.SetAttribute("value", value)
' not found, so we need to add the element, key and value
Else
Dim entry As XmlElement = cfgDoc.CreateElement("add")
entry.SetAttribute("key", key)
entry.SetAttribute("value", value)
node.AppendChild(entry)
End If
'save it
saveConfigDoc(cfgDoc, docName)
Return True
Catch
Return False
End Try
End Function
Public Function removeElement(ByVal elementKey As String) As Boolean
Try
Dim cfgDoc As XmlDocument = New XmlDocument()
loadConfigDoc(cfgDoc)
'retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings")
If (node Is Nothing) Then
Throw New System.InvalidOperationException("appSettings section not found")
Else
'XPath select setting "add" element that contains this key to remove
node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"))
saveConfigDoc(cfgDoc, docName)
End If
Return True
Catch
Return False
End Try
End Function
Private Function saveConfigDoc(ByVal cfgDoc As XmlDocument, ByVal cfgDocPath As String) As Boolean
Try
Dim writer As XmlTextWriter = New XmlTextWriter(cfgDocPath, System.Text.Encoding.ASCII)
writer.Formatting = Formatting.Indented
cfgDoc.WriteTo(writer)
writer.Flush()
writer.Close()
Return True
Catch
Return False
End Try
End Function
Private Function loadConfigDoc(ByVal cfgDoc As XmlDocument) As Boolean
Try
docName = System.AppDomain.CurrentDomain.BaseDirectory.ToString
docName += Reflection.Assembly.GetEntryAssembly.GetName.Name
docName += ".exe.config"
cfgDoc.Load(docName)
Return True
Catch e As Exception
Debug.Write(e.Message)
Return False
End Try
End Function
End Class
_________________________________________________
Let me know if it's useful更多精彩文章及讨论,请光临枫下论坛 rolia.net
Imports System.Xml
Imports System.Configuration
Imports System.Collections
Imports System.Reflection
Imports System.Diagnostics
Public Class appConfig
Private node As XmlNode = Nothing
Public docName As String = ""
Public Function SetValue(ByVal key As String, ByVal value As String) As Boolean
Dim cfgDoc As XmlDocument = New XmlDocument()
loadConfigDoc(cfgDoc)
'retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings")
If (node Is Nothing) Then
Throw New System.InvalidOperationException("appSettings section not found")
End If
Try
'XPath select setting "add" element that contains this key
Dim addElem As XmlElement = node.SelectSingleNode("//add[@key='" + key + "']")
If (addElem.HasAttributes) Then
addElem.SetAttribute("value", value)
' not found, so we need to add the element, key and value
Else
Dim entry As XmlElement = cfgDoc.CreateElement("add")
entry.SetAttribute("key", key)
entry.SetAttribute("value", value)
node.AppendChild(entry)
End If
'save it
saveConfigDoc(cfgDoc, docName)
Return True
Catch
Return False
End Try
End Function
Public Function removeElement(ByVal elementKey As String) As Boolean
Try
Dim cfgDoc As XmlDocument = New XmlDocument()
loadConfigDoc(cfgDoc)
'retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings")
If (node Is Nothing) Then
Throw New System.InvalidOperationException("appSettings section not found")
Else
'XPath select setting "add" element that contains this key to remove
node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"))
saveConfigDoc(cfgDoc, docName)
End If
Return True
Catch
Return False
End Try
End Function
Private Function saveConfigDoc(ByVal cfgDoc As XmlDocument, ByVal cfgDocPath As String) As Boolean
Try
Dim writer As XmlTextWriter = New XmlTextWriter(cfgDocPath, System.Text.Encoding.ASCII)
writer.Formatting = Formatting.Indented
cfgDoc.WriteTo(writer)
writer.Flush()
writer.Close()
Return True
Catch
Return False
End Try
End Function
Private Function loadConfigDoc(ByVal cfgDoc As XmlDocument) As Boolean
Try
docName = System.AppDomain.CurrentDomain.BaseDirectory.ToString
docName += Reflection.Assembly.GetEntryAssembly.GetName.Name
docName += ".exe.config"
cfgDoc.Load(docName)
Return True
Catch e As Exception
Debug.Write(e.Message)
Return False
End Try
End Function
End Class
_________________________________________________
Let me know if it's useful更多精彩文章及讨论,请光临枫下论坛 rolia.net