Solved

Programmatically edit the app.config

Posted on 2009-07-09
6
1,588 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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