Link to home
Start Free TrialLog in
Avatar of JackOfPH
JackOfPHFlag for Philippines

asked on

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...

Joseph....


P.S.

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




Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

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
http://www.codeproject.com/KB/dotnet/Change_AppConfig_File.aspx

Runtime Web.config / App.config Editing
http://www.eggheadcafe.com/articles/20030907.asp
    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
       XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
    ' 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
                Next
            End If
        Next
        ' Save app.config file
        XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
    End Sub

Open in new window

Avatar of JackOfPH

ASKER

Both example does not use IConfigurationSectionHandler?
You can edit it directly through xml :)
Yes, but what I want to do is edit it through IConfigurationSectionHandler...
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
 
        xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
 
        Dim xmlNode1 As XmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "Joseph", "test")
        xmlNode1.InnerText = "Ocena"
        xmlDoc.AppendChild(xmlNode1)
        xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
    End Sub

Open in new window