Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 6861 |  Welcome, Guest!   Register  Login
Home > Articles > C# > Change App.Config File

Change App.Config File

Article posted by Neeks on 7/30/2010 | Views: 5712 | Category: C# | Level: Beginner red flag


This article will explore the details related to change the App.Config file setting at runtime in Windows application.

Introduction

Sometimes we require to change the settings related to App.Config file at runtime in the application. So, we require the AppSettings for changing the App.Config File

Pre-Requisite: User should know how to add the appSettings in the App.Config fie, so user should have basic idea to create the key and set the value for the key in the AppSetting node before changing the value at runtime.



Required references

We require following the references in the application to modify the App.Config File
1. System //This is the default reference
2. System.Configuration





All Set? Now change the Config File

After adding the details related to the References we can change the AppSetting section from the App.Config file
Now, add the namespace

System.Configuration.

Code to change the AppSetting section.


C# Code

//Create the object of the Class Configuration

Configuration objConfig= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

//Key to Add in the App.Config file

objConfig.AppSettings.Settings.Add("CurrentYear"DateTime.Now.Year);

// Now, we can save the configuration file.

objConfig.Save(ConfigurationSaveMode.Modified);

// After saving the file we have to refresh the section

ConfigurationManager.RefreshSection("appSettings");


NOTE:

You will not see any changes in the App.Config file as it may be in the debug mode. So please release the application and make the changes you can see the change in the app.config file

Conclusion

This way you can manage the appSetting section of the App.Config file from the C# application at runtime.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:1 year(s)
Home page:http://www.neeks4ever.blogspot.com/
Member since:Tuesday, November 25, 2008
Level:Bronze
Status: [Member]
Biography:Keep posting and Enjoy coding...
>> Write Response - Respond to this post and get points
Related Posts

This article explains to us , how to connect with MS Access 2007 database from C# Windows application.

Here is the working example of looping through all rows of the DataTable. You can download the code and simply run it by placing to your IIS root folder. Let me explain you looping through all rows of the DataTable step wise.

In this Article we are going to learn about Data Types in C#

Nullable types are instances of the System.Nullable struct. A nullable type can represent the normal range of values for its underlying value type, plus an additional null value.

To infer about how the reference types are passed to a method and what is the outcome.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 8:20:56 AM