Runtime Web.config / App.config Editing

原创 2004年09月07日 11:14:00

Web.config configuration files and app.config project item files, which get converted to "ExecutableName.exe.config" at build time, both support the convenient appSettings section with its own read method in the System.Configuration.ConfigurationSettngs class. The appSettings section stores element name / value pairs in the format:

<add key="elementName" value="elementValue" />

You can store as many of these <add> elements as you want, read them out at runtime, and use the values in the application. If you have an item that contains multiple values and you would like to keep them together, you can store them as a single string, delimited with a pipe | or other symbol, read them out at runtime, and call the String.Split() method to parse them into a useable string array.

I often read out my appSetting values into a NameValueCollection at runtime, which provides one-shot acess to the entire collection in memory:

NameValueCollection mySettings = System.Configuration.ConmfigurationSettings.AppSettings;
string connStr = mySettings["connString"];

But what about being able to change, add, and save appSettings items while that app is running in response to user input or other actions, instead of just reading them out? Nada, Zippo, Efes!  You have to open the config file manually and add them by "hand". Well that kinda stinks, don't you think? So here's my take on a convenient little class that allows you to either modify, add or delete any appSettings element, in either your Executable, Console or ASP.NET web application at runtime, on the fly. Bear in mind of course, that if you modify a web.config on a running ASP.NET app, the ASP.NET worker process will recycle. Users currently using your app aren't exactly guaranteed to have a fun experience when this happens...

New Articles & Tips
SQL Server Reporting Services - Lessons Learned
Dr. Dotnetsky's Cool .NET Tips and Tricks # 18
.NET Compact Framework Save Signature To File
Compressed Ink for Tablet PC and Windows XP
WebService Enabling SQL Server 2005 Methods
HOWTO: Register an Assembly in the GA
.NET Framework 1.1 SP1 - Issues
Circular References / Memory Leaks /other baddies
Rsources Section Now Open for Testing!
.NET Compact Framework App.Config Workaround
<SCRIPT type=text/javascript><!-- google_ad_client = "pub-8682474657542641"; google_alternate_ad_url = ""; google_ad_width = 300; google_ad_height = 250; google_ad_format = "300x250_as"; google_ad_channel ="0679176943"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "6D99CA"; google_color_url = "008000"; google_color_text = "000000"; //--></SCRIPT> <SCRIPT src="" type=text/javascript> </SCRIPT> <IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src=";dt=1094525945859&amp;lmt=1094525945&amp;;format=300x250_as&amp;output=html&amp;channel=0679176943&amp;;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=6D99CA&amp;color_url=008000&amp;color_border=FFFFFF&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=1&amp;u_java=true" frameBorder=0 width=300 scrolling=no height=250 allowTransparency><img height="1" width="1" border="0" src="" /></IFRAME>

I'm sure the above can be improved, but it works just fine for me. Notice that if you attempt to modify an element that doesn't exist, we assume that we need to create it for you. In the downloadable solution below you'll find sample projects for a Web application and a Winforms executable, both of which contain sample code to use this class. Note that there are three subfolders under the solution when you unzip this, and the one named "appConfigWeb" needs to be made an IIS virtual directory / application. Enjoy!

Download the code that accompanies this article


Peter Bromberg began programming at Merrill Lynch, developing computerized trading programs, later becoming a Development Manager and Senior Programmer at medical and financial services firms. In 2001, he was lead developer on a jointly-funded project with Microsoft to convert COM banking services middleware to the new .NET platform. The technology tested at Microsoft Testing Lab at 10 times faster than any previous implementation. Peter has architected numerous enterprise - level business solutions with .NET, and is the co-founder of His samples at have been downloaded over 26,000 times. He is a Microsoft MVP, and is currently a Senior Developer at AspSoft, based in Orlando.
Do you have a question or comment about this article? Have a programming problem you need to solve? Post it at forums and receive immediate email notification of responses.

Runtime Web.config / App.config Editing

摘自 System;using System.Xml; using System.Confi...
  • codeangel
  • codeangel
  • 2005年07月05日 17:19
  • 1104

Runtime Web.config / App.config Editing

 Web.config configuration files and app.config project item files, which get converted to "Executabl...
  • abaowu
  • abaowu
  • 2004年09月10日 15:56
  • 1758 配置文件(Machine.config、Web.config、App.config)

Machine.config 1.该文件在Windows目录下\\framework\[version]\Config\ 2.为了提高性能,该文件只包含不同于默认值的设置...
  • ydm19891101
  • ydm19891101
  • 2016年03月28日 09:02
  • 1553

Web.config 和 App.config 区别

Web.config 和 App.config 区别 web.config是web应用程序的配置文件,为web应用程序提供相关配置。在你开发的web程序中,你可以为每一个文件夹建立一个web.c...
  • L0veIT
  • L0veIT
  • 2012年07月23日 12:48
  • 1011

操作App.config与Web.config文件 - System.Configuration方式与XMLPath方式, 超强功能类

操作App.config与Web.config文件  [csharp] view plaincopyprint? public class Conf...
  • kingmax54212008
  • kingmax54212008
  • 2015年09月11日 07:24
  • 791


一、Net操作web.config通用类(代码如下): using System; using System.Configuration; using System.Web; using S...
  • my98800
  • my98800
  • 2016年06月01日 08:18
  • 328


  • pukuimin1226
  • pukuimin1226
  • 2016年08月26日 15:14
  • 2304

数据库连接配置 app.config web.config

通过ADO.Net连接程序和SQLServer数据库的连接字符串:connectionString ="server=(local);database=Demo;integrated security...
  • bigpudding24
  • bigpudding24
  • 2014年10月15日 09:58
  • 881


c# .config配置文件
  • fallschool
  • fallschool
  • 2016年05月31日 14:53
  • 1988


app.config配置方法(Log4net v1.2.0 ) 1,app.config中   ...
  • u011776918
  • u011776918
  • 2015年04月18日 00:52
  • 3000
您举报文章:Runtime Web.config / App.config Editing