Advertisement

08.31.2007 at 01:50AM PDT, ID: 22799447
[x]
Attachment Details

Writing to app.config

Asked by SirReadAlot in Microsoft Visual Basic.Net

Tags:

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.AppSettings(key)
        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("//appSettings")

            If node Is Nothing Then
                Throw New InvalidOperationException("appSettings section not found in config file.")
            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 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("//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

        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.FileNotFoundException
                Throw New Exception("No configuration file found.", e)
            End Try
        End Function


        Private Shared Function getConfigFilePath() As String
            Return System.Reflection.Assembly.GetExecutingAssembly().Location + ".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(counter).GetValue(0).ToString()      'get the ldap path
                Console.WriteLine("Searching: " & domain)
                FindADObjects(domain, thisldapPath, adObjects)
                Console.WriteLine("")

                'get current time
                StartTime = Format(DateTime.Now, "yyyyMMddHHmmssZ")
                Console.WriteLine(StartTime)
                'Console.ReadLine()

                ConfigSettings.WriteSetting("WhenCreated", StartTime)====called code here
                'SetApplicationValue("whenCreated", StartTime)
                'If _email Then SendMail(emailBody)
                Console.ReadLine()

            Next
        End Sub



========================should update value="20070829000000.0Z"with 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
[+][-]08.31.2007 at 02:36AM PDT, ID: 19806838

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.31.2007 at 02:42AM PDT, ID: 19806850

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.31.2007 at 03:15AM PDT, ID: 19806944

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Visual Basic.Net
Tags: vb.net
Sign Up Now!
Solution Provided By: ToFro
Participating Experts: 2
Solution Grade: A
 
 
[+][-]08.31.2007 at 03:17AM PDT, ID: 19806953

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.31.2007 at 03:18AM PDT, ID: 19806957

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.31.2007 at 05:32AM PDT, ID: 19807531

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.31.2007 at 05:37AM PDT, ID: 19807559

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.31.2007 at 08:00AM PDT, ID: 19808770

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628