Click here to Skip to main content
Licence CPOL
First Posted 24 Mar 2007
Views 99,157
Downloads 1,509
Bookmarked 35 times

Modifying app.config File

By | 22 Aug 2007 | Article
An article on how to change appSettings Key values of the app.config file
Screenshot - updatearticleForm.jpg

Introduction

This article will help you to change the <appSettings> values when you are deploying the application for the first time. At the time of application installation, we need to set application configuration parameters at the first time only. For example: When we change our database to another server, we can change Database Server Configuration Information into application Configuration file. At that time, we need to again modify the app.Config file by using our user Interface.

Background

The main purpose of this article is to Configure the application Configuration Settings. When we are Installing Windows Application, we have to update the app.Config file according to our requirements.

Using the Code

Here I have created a class that will help you to change the appSettings value. For that, you have to use this function with two parameters UpdateAppSettings().

You can call this function:

AppConfigFileSettings.UpdateAppSettings("DBServerName", txtNewKeyvalue.text)

You can update appSettings in this way:

// Class Name: AppConfigFileSettings
// Purpose: To Change appSettings Values

Imports System.Configuration
Imports System.Xml
''' <summary>
''' AppConfigFileSettings: This class is used to Change the 
''' AppConfigs Parameters at runtime through User Interface
''' </summary>
''' <remarks></remarks>
Public Class AppConfigFileSettings
    ''' <summary>
    ''' UpdateAppSettings: It will update the app.Config file AppConfig key values
    ''' </summary>
    ''' <param name="KeyName">AppConfigs KeyName</param>
    ''' <param name="KeyValue">AppConfigs KeyValue</param>
    ''' <remarks></remarks>
    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
End Class 
app.config File  
<configuration>
  <appSettings>
     <add key="DBServerName" value="Localhost"></add>
     <add key="DatabaseName" value="TestDB"></add>
     <add key="DatabaseUserID" value="sa"></add>
     <add key="DatabasePwd" value="sa"></add>
  </appSettings>
</configuration>

Points of Interest

  1. Once you run this application to change the appSettings value, it will not affect an application until you Exit the application and run it again.
  2. This article shows how to modify the app.config file. But how does this code help other users? - That depends on user requirements and your point of view.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jatin Prajapati

Team Leader

India India

Member

Jatin is Working in .Net Technology Since 2006. He has Completed Master of Science Degree in Information Technology. He Likes to learn Cutting edge technologies. He had good Skills in Asp.net, Vb.net,C#.Net, Crystal Reports,GDI+, Atlas.Net, SQL Server,IIS Admin,VSS Admin, Application Architecture Designing.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionConnection String is not changing... only retrieving the values. PinmemberSaroj Kumar Sahu19:54 5 Apr '12  
Questionhow to reference the data in the appconfig to conn as new sqlconnection? PinmemberMember 83988014:08 27 Jan '12  
GeneralMy vote of 1 Pinmembersharry2kool19:55 23 May '11  
GeneralGreat Post, but I'm still having alittle trouble PinmemberQEBecky5:15 10 Aug '10  
GeneralRe: Great Post, but I'm still having alittle trouble PinmemberJatin Prajapati22:30 12 Aug '10  
GeneralRe: Great Post, but I'm still having alittle trouble PinmemberQEBecky3:08 13 Aug '10  
GeneralRe: Great Post, but I'm still having alittle trouble PinmemberQEBecky3:52 13 Aug '10  
GeneralRe: Great Post, but I'm still having alittle trouble PinmemberSerGOsin5:08 7 May '12  
GeneralSystem.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. PinmemberMember 11991291:59 30 Jul '10  
Generalchange the appicationSettings node Value in app.config file at runtime PinmemberRajeshgut20:34 30 Jan '09  
QuestionWhy not use ConfigurationManager CLASS PinmemberDavid Hay5:27 27 Mar '07  
AnswerRe: Why not use ConfigurationManager CLASS PinmemberJatin Prajapati5:31 27 Mar '07  
AnswerRe: Why not use ConfigurationManager CLASS Pinmemberwaelahmed21:07 11 May '07  
GeneralRe: Why not use ConfigurationManager CLASS PinmemberQEBecky5:16 10 Aug '10  
GeneralRe: Why not use ConfigurationManager CLASS PinmemberSerGOsin5:10 7 May '12  
Generala little more complex approach PinmemberAlexandru Stanciu10:09 26 Mar '07  
GeneralRe: a little more complex approach PinmemberJatin Prajapati17:47 26 Mar '07  
GeneralRe: a little more complex approach PinsitebuilderShog97:15 27 Mar '07  
AnswerYou almost certainly don't want to do this. PinsitebuilderShog98:17 24 Mar '07  
GeneralRe: You almost certainly don't want to do this. PinmemberJatin Prajapati8:27 24 Mar '07  
GeneralRe: You almost certainly don't want to do this. PinsitebuilderShog98:35 24 Mar '07  
GeneralRe: You almost certainly don't want to do this. PinmemberJatin Prajapati8:39 24 Mar '07  
GeneralRe: You almost certainly don't want to do this. PinsitebuilderShog98:55 24 Mar '07  
GeneralRe: You almost certainly don't want to do this. PinmemberJatin Prajapati8:59 24 Mar '07  
GeneralRe: You almost certainly don't want to do this. PinmemberJatin Prajapati9:03 24 Mar '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 22 Aug 2007
Article Copyright 2007 by Jatin Prajapati
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid