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 = "http://www.eggheadcafe.com/alternateads.asp"; 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="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </SCRIPT> <IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-8682474657542641&amp;dt=1094802654559&amp;lmt=1094802654&amp;alternate_ad_url=http%3A%2F%2Fwww.eggheadcafe.com%2Falternateads.asp&amp;format=300x250_as&amp;output=html&amp;channel=0679176943&amp;url=http%3A%2F%2Fdev.csdn.net%2Farticle%2F36%2F36055.shtm&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="http://pagead2.googlesyndication.com/pagead/imp.gif?client=ca-pub-8682474657542641&dt=1094802654559&lmt=1094802654&alternate_ad_url=http%3A%2F%2Fwww.eggheadcafe.com%2Falternateads.asp&format=300x250_as&output=html&channel=0679176943&url=http%3A%2F%2Fdev.csdn.net%2Farticle%2F36%2F36055.shtm&color_bg=FFFFFF&color_text=000000&color_link=6D99CA&color_url=008000&color_border=FFFFFF&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_java=true&event=noiframe" /></IFRAME><IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-8682474657542641&amp;dt=1094525945859&amp;lmt=1094525945&amp;alternate_ad_url=http%3A%2F%2Fwww.eggheadcafe.com%2Falternateads.asp&amp;format=300x250_as&amp;output=html&amp;channel=0679176943&amp;url=http%3A%2F%2Fwww.eggheadcafe.com%2Farticles%2F20030907.asp&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="http://pagead2.googlesyndication.com/pagead/imp.gif?client=ca-pub-8682474657542641&dt=1094525945859&lmt=1094525945&alternate_ad_url=http%3A%2F%2Fwww.eggheadcafe.com%2Falternateads.asp&format=300x250_as&output=html&channel=0679176943&url=http%3A%2F%2Fwww.eggheadcafe.com%2Farticles%2F20030907.asp&color_bg=FFFFFF&color_text=000000&color_link=6D99CA&color_url=008000&color_border=FFFFFF&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=1&u_java=true&event=noiframe" /></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 eggheadcafe.com. His samples at gotdotnet.com have been downloaded over 26,000 times. He is a Microsoft MVP, and is currently a Senior Developer at AspSoft, based in Orlando.
MVPlogo.gif
Do you have a question or comment about this article? Have a programming problem you need to solve? Post it at eggheadcafe.com forums and receive immediate email notification of responses.

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

原文出处:http://www.cnblogs.com/zhaobl/archive/2011/09/29/appsettingHelper.html 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

C#项目中关于多个程序集下App.config文件的问题

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

asp.net 配置文件(Machine.config、Web.config、App.config)

Machine.config 1.该文件在Windows目录下\Microsoft.net\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

web.config中httpRunTime的属性

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

Runtime Web.config / App.config Editing

摘自http://www.eggheadcafe.com/articles/20030907.aspusing 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

android6.0源码分析之Runtime的初始化

Android运行时是android中至关重要的,它是在Init进程和Zygote进程的中间进行初始化的,本文将分析其具体的初始化过程,并将分析非Zygore模式下的RuntimeInit过程....
  • yangzhihuiguming
  • yangzhihuiguming
  • 2016-06-17 09:59:09
  • 7345

关于framework和runtime

这是两个不同的概念,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
举报原因:
原因补充:

(最多只允许输入30个字)