Solved

Programmatically edit the app.config

Posted on 2009-07-09
6
1,549 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

19 Experts available now in Live!

Get 1:1 Help Now