Hi experts,
I used this resource
http://ryanfarley.com/blog/archive/2004/07/13/879.aspx(now in vb.net) to write to my app.config at runtime.
so far there are no errors only that the app.config file is not updated at runtime.
here is the code
==========================
==========
Imports System
Imports System.Xml
Imports System.Configuration
Imports System.Reflection
Namespace Mace.IT
Public Class ConfigSettings
Private Sub New()
End Sub
Public Shared Function ReadSetting(ByVal key As String) As String
Return ConfigurationSettings.AppS
ettings(ke
y)
End Function
Public Shared Sub WriteSetting(ByVal key As String, ByVal value As String)
' load config document for current assembly
Dim doc As XmlDocument = loadConfigDocument()
' retrieve appSettings node
Dim node As XmlNode = doc.SelectSingleNode("//ap
pSettings"
)
If node Is Nothing Then
Throw New InvalidOperationException(
"appSettin
gs section not found in config file.")
End If
Try
' select the 'add' element that contains the key
Dim elem As XmlElement = DirectCast(node.SelectSing
leNode(Str
ing.Format
("//add[@k
ey='{0}']"
, key)), XmlElement)
If elem Is 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 Shared Sub RemoveSetting(ByVal key As String)
' load config document for current assembly
Dim doc As XmlDocument = loadConfigDocument()
' retrieve appSettings node
Dim node As XmlNode = doc.SelectSingleNode("//ap
pSettings"
)
Try
If node Is Nothing Then
Throw New InvalidOperationException(
"appSettin
gs section not found in config file.")
Else
' remove 'add' element with coresponding key
node.RemoveChild(node.Sele
ctSingleNo
de(String.
Format("//
add[@key='
{0}']", key)))
doc.Save(getConfigFilePath
())
End If
Catch e As NullReferenceException
Throw New Exception(String.Format("T
he key {0} does not exist.", key), e)
End Try
End Sub
Private Shared Function loadConfigDocument() As XmlDocument
Dim doc As XmlDocument = Nothing
Try
doc = New XmlDocument
doc.Load(getConfigFilePath
())
Return doc
Catch e As System.IO.FileNotFoundExce
ption
Throw New Exception("No configuration file found.", e)
End Try
End Function
Private Shared Function getConfigFilePath() As String
Return System.Reflection.Assembly
.GetExecut
ingAssembl
y().Locati
on + ".config"
End Function
End Class
End Namespace
==============i called it here======================
Public Sub Search()
Dim ldapPath As String = String.Empty
Dim adObjects As SortedList
Dim domain As String = String.Empty
Dim domainController As String
Dim domainControllers As ArrayList
Dim emailBody As String
Dim thisldapPath As String = String.Empty
Dim StartTime As String
Init()
adObjects = New SortedList
For counter As Integer = 0 To _objDomains.Count - 1
domain = _objDomains.GetKey(counter
).ToString
().ToLower
'get the domain name
ldapPath = _objDomains.GetValues(coun
ter).GetVa
lue(0).ToS
tring() 'get the ldap path
Console.WriteLine("Searchi
ng: " & domain)
FindADObjects(domain, thisldapPath, adObjects)
Console.WriteLine("")
'get current time
StartTime = Format(DateTime.Now, "yyyyMMddHHmmssZ")
Console.WriteLine(StartTim
e)
'Console.ReadLine()
ConfigSettings.WriteSettin
g("WhenCre
ated", StartTime)====called code here
'SetApplicationValue("when
Created", StartTime)
'If _email Then SendMail(emailBody)
Console.ReadLine()
Next
End Sub
========================sh
ould update value="20070829000000.0Z"w
ith currenttime===========
<appSettings>
<add key="Username" value="" />
<add key="Password" value="" />
<add key="Debug" value="1" />
<add key="Email" value="0" />
<add key="WhenCreated" value="20070829000000.0Z" />
==========================
==========
when i place breakpoints, i can see the current time value but how come its not updating the app.config?
thanks
Start Free Trial