Runtime Web.config / App.config Editing

转载 2004年09月10日 15:56: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 Tips and Tricks # 18
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
Framework 1.1 SP1 - Issues
Circular References / Memory Leaks /other baddies
Rsources Section Now Open for Testing!
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=1094802654559&amp;lmt=1094802654&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_java=true" frameBorder=0 width=300 scrolling=no height=250 allowTransparency><img height="1" width="1" border="0" src="" /></IFRAME><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.

App.config 中读写appSettings、system.serviceModel终结点,以及自定义配置节

原文出处: 1.appSettings的读写 但...
  • chelen_jak
  • chelen_jak
  • 2012-11-16 14:26:47
  • 6681

VS 提示:请考虑使用 app.config 将程序集“XXX”从版本“XX”重新映射到版本“XX”,以解决冲突并消除警告。

具体提示如下: 请考虑使用 app.config 将程序集“System.Web.Http.WebHost, Culture=neutral, PublicKeyToken=31bf3856ad3...
  • Dandelion_drq
  • Dandelion_drq
  • 2017-06-14 14:34:57
  • 2022


在项目中我们会经常用到App.config文件,有的是自动生成的,比如引用webservice、wcf服务时生成;也有手动建立的配置文件直接默认名就为app.config。 这些配置有的保存当前程序...
  • yysyangyangyangshan
  • yysyangyangyangshan
  • 2013-01-17 15:29:07
  • 12879 配置文件(Machine.config、Web.config、App.config)

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

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:00
  • 1772


配置httpRuntime也可以让FileUpload上传更大的文件,不过设置太大了会因用户将大量文件传递到该服务器而导致的拒绝服务攻击(属性有说明)="true|false"            ...
  • wygyhm
  • wygyhm
  • 2008-08-23 16:09:00
  • 21250

Runtime Web.config / App.config Editing

摘自 System;using System.Xml; using System.Confi...
  • codeangel
  • codeangel
  • 2005-07-05 17:19:00
  • 1117

Web.config 和 App.config 区别

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


  • yangzhihuiguming
  • yangzhihuiguming
  • 2016-06-17 09:59:09
  • 7345


这是两个不同的概念,framework是开发框架,runtime是运行时所需环境。 比较直观的区别是:dotNET_framework_110_setup.exe有106M,而dotnet_runti...
  • wwwsq
  • wwwsq
  • 2004-07-05 15:30:00
  • 2088
您举报文章:Runtime Web.config / App.config Editing