Go to DotNetFunda.com
 Online : 1967 |  Welcome, Guest!   Login
 
Home > Articles > C# > Change App.Config File

Submit Article | Articles Home | Search Articles |

Change App.Config File

red flag  Posted on: 7/30/2010 2:19:24 PM by Neeks | Views: 721 | Category: C# | Level: Beginner


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.

Found interesting? Add this to:

| More



Please Sign In to vote for this post.

 
Latest post(s) from Neeks

Latest Articles
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...

Submit Article

About Us | The Team | Advertise | Contact 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. | 9/7/2010 12:31:24 AM