Programmatically edit the app.config

I read in some article in the web, that I can edited the app.config through the use of IConfigurationSectionHandler.

Can you please explain to me how it works? I am really lost, I can't understand it...

Please, provide me a detailed explanation.

For example....

This line of code will write to the app.config file.

This line of code will loop through all the nodes in the app.config file....

Did you get what I mean?

Please, I really need your help...



Please don't give me some links to visit, because I already tried those and I can't understand it.

LVL 15
Who is Participating?
DhaestConnect With a Mentor Commented:
I can't help you any further than, because I did it with xml. (although I don't find it very good to create/update/delete something in the app.config in runtime)

Understanding Section Handlers - App.config File
By Palanisamy Veerasingam
This article explains configuration section handlers defined in the System.Configuration namespace and explains how to create custom sections handlers by implemeting the IConfigurationSectionHandler interface.
I know that you said no links, but this article explains everything and gives also the coding (and comments what the line does)
Modifying app.config File

Runtime Web.config / App.config Editing
    Public Shared Sub UpdateAppSettings_
        (ByVal KeyName As String, ByVal KeyValue As String)
    '  AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 
    ' This will get the app.config file path from Current application Domain
        Dim XmlDoc As New XmlDocument()
    ' Load XML Document
    ' Navigate Each XML Element of app.Config file
        For Each xElement As XmlElement In XmlDoc.DocumentElement
            If xElement.Name = "appSettings" Then
                ' Loop each node of appSettings Element 
                ' xNode.Attributes(0).Value , Mean First Attributes of Node , 
                ' KeyName Portion
                ' xNode.Attributes(1).Value , Mean Second Attributes of Node,
                ' KeyValue Portion
                 For Each xNode As XmlNode In xElement.ChildNodes
                    If xNode.Attributes(0).Value = KeyName Then
                        xNode.Attributes(1).Value = KeyValue
                    End If
            End If
        ' Save app.config file
    End Sub

Open in new window

JackOfPHAuthor Commented:
Both example does not use IConfigurationSectionHandler?
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

You can edit it directly through xml :)
JackOfPHAuthor Commented:
Yes, but what I want to do is edit it through IConfigurationSectionHandler...
JackOfPHAuthor Commented:
ok, lets try it your way....

How can I add new element? to the app.config file?

I tried this code but no luck...
Public Shared Sub UpdateSetting(ByVal Keyname As String, ByVal keyvalue As String)
        Dim xmlDoc As New XmlDocument
        Dim xmlNode1 As XmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "Joseph", "test")
        xmlNode1.InnerText = "Ocena"
    End Sub

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.