Login
Register
Tutorials
Forum
Career Development
Resources
Reviews
Jobs
Interview
Communities
Projects
Training
Silverlight Games
|
Bookmarks
|
New Members FAQ
|
Mentor
|
Code Converter
|
IT Companies
|
Peer Appraisal
|
Members
|
Revenue Sharing
|
Computer Jokes
|
New Posts
|
Social
|
Talk to Webmaster
Tony John
Facebook
Google+
Twitter
LinkedIn
Online Members
Rakesh Chaubey
naveensanagasetti
Danasegarane.A
jignesh
abhinav
monika
Ranipriya
Ultimaterengan
malika
HemzChand
More...
Join our online
Google+ community
for Bloggers, Content Writers and Webmasters
Resources
»
Code Snippets
»
C# Syntax
Dynamic App Settings
Posted Date:
25-Mar-2009
Category:
C# Syntax
Author:
Praveen
Member Level:
Gold
Points
: 10
Here is a Small Example how to write to App.config file for windows application.
This code Changes your app.config file at run time. The location that this sample takes your bin folder project.exe.config.
Ex: if your project name is sample and it it should contain app.config file and this sample writes to the config file that is created in your bin folder.
This code Accepts Parameter
<--appSettings-->
ex: <--add Key="Sample" value=""-->
<--/appSettings-->
1. NewValue - Value to Change (The Value that you want to change for the key>
2. KeyName - The Name of the Key in AppSettings (In the above example we added a key called 'Sample' so, we need to send Sample as KeyName)
3. Settings - The Settings Name the Key Exits(ex: In the above Example our Key is in 'appSettings' so, we need to send appSettings' as Settings)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Main.App_Code.Common;
using System.Configuration;
class ConfigFileSettings
{
#region Public Methods
public bool WritetoConfigFile(string NewValue,string KeyName,string Settings)
{
string configpath = System.Reflection.Assembly.GetExecutingAssembly().Location + ".config";
if (!(System.IO.File.Exists(configpath)))
throw new Exception("Application Configuration File Not Exists" + Environment.NewLine + "Please Contact Administrator");
WriteAppSet(ref configpath, ref NewValue,ref KeyName,ref Settings);
return true;
}
#endregion
#region Private Methods
private void WriteAppSet(ref string ConfigPath,ref string NewValue,ref string KeyName,ref string Settings)
{
XmlDocument objXml=null;
XmlNode nd = null;
XmlElement el = null;
XmlTextWriter tw = null;
try
{
objXml = new XmlDocument();
objXml.Load(ConfigPath);
nd = objXml.SelectSingleNode("//" + Settings);
if (nd == null)
{
throw new Exception("Please Add "+KeyName+" Key in Application Configuration File");
}
el = (XmlElement)nd.SelectSingleNode("//add[@key='" + KeyName + "']");
if (el != null)
{
el.SetAttribute("value", NewValue);
}
tw = new XmlTextWriter(ConfigPath, null);
tw.Formatting = Formatting.Indented;
objXml.WriteTo(tw);
tw.Flush();
tw.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (tw != null)
tw = null;
if (el != null)
el = null;
if (nd != null)
nd = null;
if (objXml != null)
objXml = null;
}
}
#endregion
}
Attachments
DynamicallyChangeAppSettings
(27179-2542-DynamicAppsettings.doc)
Did you like this resource? Share it with your friends and show your love!
Tweet
Responses to "Dynamic App Settings"
No responses found. Be the first to respond...
Feedbacks
Post Comment:
Notify me by email when others post comments to this article.
Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
No HTML formatting and links to other web sites are allowed.
This is a strictly moderated site. Absolutely no spam allowed.
Name:
Sign In
to fill automatically.
Email:
(Will not be published, but
required
to validate comment)
Type the numbers and letters shown on the left.
Next Resource:
Create Excel file from C#
Previous Resource:
Sine Wave or Square Wave Can be produced using C#
Return to Resources
Post New Resource
Category:
C# Syntax
Post resources and
earn money
!
More Resources
Create Excel file from C#
Code for sending E-Mail
How To check whether a Port Number is Valid or not?
Exclude some object while serializing
All Date formats in C#
How to Get Hard Disk Serial Number?
Popular Tags
Tag posting guidelines
Search Tags
Dynamically Change App Settings Key Value
.
Follow us on Twitter:
https://twitter.com/dotnetspider
Active Members
Today
Ultimaterengan
(2)
HemzChand
(1)
Last 7 Days
baskar
(295)
Ultimaterengan
(263)
Rakesh Chaubey
(178)
more...
Awards & Gifts
Email subscription
.NET Jobs
.NET Articles
.NET Forums
Articles Rss Feeds
Forum Rss Feeds
About Us
Contact Us
Copyright
Privacy Policy
Terms Of Use
Revenue Sharing sites
Advertise
Talk to
Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.