Solved

Programmatically edit the app.config

Posted on 2009-07-09
6
1,556 Views
Last Modified: 2012-05-07
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.




0
Comment
Question by:JackOfPH
  • 3
  • 3
6 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 24811556
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

0
 
LVL 15

Author Comment

by:JackOfPH
ID: 24811594
Both example does not use IConfigurationSectionHandler?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24811604
You can edit it directly through xml :)
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Author Comment

by:JackOfPH
ID: 24811628
Yes, but what I want to do is edit it through IConfigurationSectionHandler...
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 24811735
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.
http://www.codeproject.com/KB/aspnet/ConfigSections.aspx?display=Print
0
 
LVL 15

Author Comment

by:JackOfPH
ID: 24811752
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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now