Solved

Programmatically edit the app.config

Posted on 2009-07-09
6
1,563 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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 …
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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