Click here to Skip to main content
15,883,901 members
Articles / Programming Languages / Visual Basic
Article

Modifying app.config File

Rate me:
Please Sign up or sign in to vote.
2.74/5 (23 votes)
22 Aug 2007CPOL1 min read 214.8K   4.7K   38   36
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:

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

You can update appSettings in this way:

VB.NET
// 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)


Written By
Team Leader
India India
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 has good Skills in Asp.net, Vb.net,C#.Net, Crystal Reports,GDI+, Ajax, WCF, Silverlight SQL Server,IIS Admin,TFA ,Application Architecture Designing.

Comments and Discussions

 
Bugexeception Pin
Member 1907786-Oct-15 20:33
Member 1907786-Oct-15 20:33 
GeneralThanks a lot Pin
AnandIndore3-Jan-14 23:21
AnandIndore3-Jan-14 23:21 
QuestionConnection String is not changing... only retrieving the values. Pin
Saroj Kumar Sahu5-Apr-12 19:54
Saroj Kumar Sahu5-Apr-12 19:54 
Questionhow to reference the data in the appconfig to conn as new sqlconnection? Pin
Member 839880127-Jan-12 4:08
Member 839880127-Jan-12 4:08 
GeneralMy vote of 1 Pin
shareque23-May-11 19:55
shareque23-May-11 19:55 
GeneralGreat Post, but I'm still having alittle trouble Pin
QEBecky10-Aug-10 5:15
QEBecky10-Aug-10 5:15 
GeneralRe: Great Post, but I'm still having alittle trouble Pin
Jatin Prajapati12-Aug-10 22:30
Jatin Prajapati12-Aug-10 22:30 
GeneralRe: Great Post, but I'm still having alittle trouble Pin
QEBecky13-Aug-10 3:08
QEBecky13-Aug-10 3:08 
GeneralRe: Great Post, but I'm still having alittle trouble Pin
QEBecky13-Aug-10 3:52
QEBecky13-Aug-10 3:52 
GeneralRe: Great Post, but I'm still having alittle trouble Pin
SerGOsin7-May-12 5:08
SerGOsin7-May-12 5:08 
GeneralSystem.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. Pin
Neeraj Kamboj30-Jul-10 1:59
Neeraj Kamboj30-Jul-10 1:59 
Generalchange the appicationSettings node Value in app.config file at runtime Pin
Rajeshgut30-Jan-09 20:34
Rajeshgut30-Jan-09 20:34 
QuestionWhy not use ConfigurationManager CLASS Pin
David Hay27-Mar-07 5:27
David Hay27-Mar-07 5:27 
Why didn't you just use the System.Configuration.ConfigurationManager class? This class is provided for accessing and updating configuration files. Then your Sub would only need four lines of code and would have looked like this:

Public Shared Sub UpdateAppSettings(ByVal KeyName As String, ByVal KeyValue As String)

    ' Get the configuration file.
    Dim config As System.Configuration.Configuration = _
        ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        
    ' Update the setting.
    config.AppSettings.Settings(KeyName) = KeyValue
        
    ' Save the configuration file.
    config.Save(ConfigurationSaveMode.Modified)
        
    ' Force a reload of the changed section.
    ConfigurationManager.RefreshSection("appSettings")
    
End Sub


And thanks to the last line, you have the added advantage that you do not need to stop and restart the application for the change to take effect!

Regards
David
AnswerRe: Why not use ConfigurationManager CLASS Pin
Jatin Prajapati27-Mar-07 5:31
Jatin Prajapati27-Mar-07 5:31 
AnswerRe: Why not use ConfigurationManager CLASS Pin
waelahmed11-May-07 21:07
waelahmed11-May-07 21:07 
GeneralRe: Why not use ConfigurationManager CLASS Pin
QEBecky10-Aug-10 5:16
QEBecky10-Aug-10 5:16 
GeneralRe: Why not use ConfigurationManager CLASS Pin
SerGOsin7-May-12 5:10
SerGOsin7-May-12 5:10 
Generala little more complex approach Pin
Alexandru Stanciu26-Mar-07 10:09
Alexandru Stanciu26-Mar-07 10:09 
GeneralRe: a little more complex approach Pin
Jatin Prajapati26-Mar-07 17:47
Jatin Prajapati26-Mar-07 17:47 
GeneralRe: a little more complex approach Pin
Shog927-Mar-07 7:15
sitebuilderShog927-Mar-07 7:15 
AnswerYou almost certainly don't want to do this. Pin
Shog924-Mar-07 8:17
sitebuilderShog924-Mar-07 8:17 
GeneralRe: You almost certainly don't want to do this. Pin
Jatin Prajapati24-Mar-07 8:27
Jatin Prajapati24-Mar-07 8:27 
GeneralRe: You almost certainly don't want to do this. Pin
Shog924-Mar-07 8:35
sitebuilderShog924-Mar-07 8:35 
GeneralRe: You almost certainly don't want to do this. Pin
Jatin Prajapati24-Mar-07 8:39
Jatin Prajapati24-Mar-07 8:39 
GeneralRe: You almost certainly don't want to do this. Pin
Shog924-Mar-07 8:55
sitebuilderShog924-Mar-07 8:55 

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

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