Imports System
Imports System.Xml
Imports System.Configuration
Imports System.Reflection
Module ReadandWriteinappconfig
Public Function ReadSetting(ByVal key As String) As String
Return System.Configuration.ConfigurationSettings.AppSettings(key)
End Function
Public Sub WriteSetting(ByVal key As String, ByVal value As String, ByVal Settingvalue As String)
' load config document for current assembly
Dim doc As XmlDocument = loadConfigDocument()
Dim SettingType As String
If Val(Settingvalue) = 0 Then
SettingType = "mail"
ElseIf Val(Settingvalue) = 1 Then
SettingType = "app"
ElseIf Val(Settingvalue) = 2 Then
SettingType = "password"
End If
Dim node As XmlNode = doc.SelectSingleNode("//" & SettingType & "Settings")
If node Is Nothing Then
' Throw New InvalidOperationException("appSettings section not found in config file.")
Dim xmlEl As XmlElement = doc.CreateElement(SettingType & "Settings")
doc.DocumentElement.AppendChild(xmlEl)
node = doc.SelectSingleNode("//" & SettingType & "Settings")
End If
Try
' select the 'add' element that contains the key
Dim elem As XmlElement = DirectCast(node.SelectSingleNode(String.Format("//add[@key='{0}']", key)), XmlElement)
If elem IsNot Nothing Then
' add value for key
elem.SetAttribute("value", value)
Else
' key was not found so create the 'add' element
' and set it's key/value attributes
elem = doc.CreateElement("add")
elem.SetAttribute("key", key)
elem.SetAttribute("value", value)
node.AppendChild(elem)
End If
doc.Save(getConfigFilePath())
Catch
Throw
End Try
End Sub
Public Sub RemoveSetting(ByVal key As String, ByVal Settingvalue As String)
' load config document for current assembly
Dim doc As XmlDocument = loadConfigDocument()
Dim SettingType As String
' retrieve appSettings node
If Val(Settingvalue) = 0 Then
SettingType = "mail"
ElseIf Val(Settingvalue) = 1 Then
SettingType = "app"
End If
Dim node As XmlNode = doc.SelectSingleNode("//" & SettingType & "Settings")
'Dim node As XmlNode = doc.SelectSingleNode("//appSettings")
Try
If node Is Nothing Then
Throw New InvalidOperationException("appSettings section not found in config file.")
Else
' remove 'add' element with coresponding key
node.RemoveChild(node.SelectSingleNode(String.Format("//add[@key='{0}']", key)))
doc.Save(getConfigFilePath())
End If
Catch e As NullReferenceException
Throw New Exception(String.Format("The key {0} does not exist.", key), e)
End Try
End Sub
Public Function loadConfigDocument() As XmlDocument
Dim doc As XmlDocument = Nothing
Try
doc = New XmlDocument()
doc.Load(getConfigFilePath())
Return doc
Catch e As System.IO.FileNotFoundException
Throw New Exception("No configuration file found.", e)
End Try
End Function
Private Function getConfigFilePath() As String
Return Assembly.GetExecutingAssembly().Location & ".config"
End Function
Public Function fGetSetting(ByVal key As String, ByVal Settingvalue As String) As Boolean
Dim returnstring As Boolean = False
Dim doc As XmlDocument = loadConfigDocument()
Dim SettingType As String
If Val(Settingvalue) = 0 Then
SettingType = "mail"
ElseIf Val(Settingvalue) = 1 Then
SettingType = "app"
End If
Dim node As XmlNode = doc.SelectSingleNode("//" & SettingType & "Settings")
If node Is Nothing Then
'Throw New InvalidOperationException("appSettings section not found in config file.")
Dim xmlEl As XmlElement = doc.CreateElement(SettingType & "Settings")
doc.DocumentElement.AppendChild(xmlEl)
node = doc.SelectSingleNode("//" & SettingType & "Settings")
End If
Try
Dim elem As XmlElement = DirectCast(node.SelectSingleNode(String.Format("//add[@key='{0}']", key)), XmlElement)
If elem IsNot Nothing Then
returnstring = True
Else
returnstring = False
End If
Return returnstring
Catch
Throw
End Try
End Function
Public Function fGetPasswordFromSetting(ByVal key As String, ByVal Settingvalue As String) As String
Dim returnstring As String = ""
Dim doc As XmlDocument = loadConfigDocument()
Dim SettingType As String
If Val(Settingvalue) = 0 Then
SettingType = "mail"
ElseIf Val(Settingvalue) = 1 Then
SettingType = "app"
ElseIf Val(Settingvalue) = 2 Then
SettingType = "password"
End If
Dim node As XmlNode = doc.SelectSingleNode("//" & SettingType & "Settings")
If node Is Nothing Then
'Throw New InvalidOperationException("appSettings section not found in config file.")
Dim xmlEl As XmlElement = doc.CreateElement(SettingType & "Settings")
doc.DocumentElement.AppendChild(xmlEl)
node = doc.SelectSingleNode("//" & SettingType & "Settings")
End If
Try
Dim elem As XmlElement = DirectCast(node.SelectSingleNode(String.Format("//add[@key='{0}']", key)), XmlElement)
If elem IsNot Nothing Then
returnstring = elem.GetAttribute("value")
Else
returnstring = ""
End If
Return returnstring
Catch
Throw
End Try
End Function
Public Function fGetSMTPFromSetting(ByVal key As String, ByVal Settingvalue As String) As String
Dim returnstring As String = ""
Dim doc As XmlDocument = loadConfigDocument()
Dim SettingType As String
If Val(Settingvalue) = 0 Then
SettingType = "mail"
ElseIf Val(Settingvalue) = 1 Then
SettingType = "app"
ElseIf Val(Settingvalue) = 2 Then
SettingType = "password"
End If
Dim node As XmlNode = doc.SelectSingleNode("//" & SettingType & "Settings")
If node Is Nothing Then
'Throw New InvalidOperationException("appSettings section not found in config file.")
Dim xmlEl As XmlElement = doc.CreateElement(SettingType & "Settings")
doc.DocumentElement.AppendChild(xmlEl)
node = doc.SelectSingleNode("//" & SettingType & "Settings")
End If
Try
Dim elem As XmlElement = DirectCast(node.SelectSingleNode(String.Format("//add[@key='{0}']", key)), XmlElement)
If elem IsNot Nothing Then
returnstring = elem.GetAttribute("value")
Else
returnstring = ""
End If
Return returnstring
Catch
Throw
End Try
End Function
End Module
Subscribe to:
Post Comments (Atom)
0 comments
Post a Comment