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.

Asp.Net 之 Web.config 配置文件详解

转载地址:http://www.cnblogs.com/xinaixia/p/4299373.html   在asp.net中配置文件名一般默认是web.config。每个web.config文件都...
  • Humourer
  • Humourer
  • 2017年07月26日 17:15
  • 1440

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

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

在Web.config或App.config中的添加自定义配置

.Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持。最近看到一些项目中还在自定义xml文件做程序的配置,所以忍...
  • pukuimin1226
  • pukuimin1226
  • 2016年08月26日 15:14
  • 2006

在web.config和app.config文件中增加自定义配置节点的方法

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

Web.config配置文件详解(新手必看)

花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法。很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点。所以这里只介绍一些比较常用的节点。 xm...
  • u014194763
  • u014194763
  • 2016年07月04日 14:42
  • 4934

web.config中配置数据库连接的两种方式

在网站开发中,数据库操作是经常要用到的操作,ASP.NET中一般做法是在web.config中配置数据库连接代码,然后在程序中调用数据库连接代码,这样做的好处就是当数据库连接代码需要改变的时候,我们只...
  • qq_26819733
  • qq_26819733
  • 2017年04月18日 19:03
  • 1176

Web.config 加密及解密

Web应用通常使用web.config存储数据库连接信息及应用的一些基础配置信息。为了安全性,我们采用一些加密手段来加密一些敏感信息,比如数据库连接字符串。 .NET 自带了一个aspnet_reg...
  • afandaafandaafanda
  • afandaafandaafanda
  • 2015年09月01日 22:04
  • 1726

如何让虚拟目录里面的webconfig不继承网站的设置

必須在上一层虚拟目录(如根目录)所在的Web.config加上  如:   方法如下:  程序代码            如此下一层的虚拟目录就可以达到你要的需求了。 下面再转...
  • killcwd
  • killcwd
  • 2016年01月28日 15:03
  • 726

net下多个应用之间的web.config冲突的解决办法

最近在项目中需要在应用中增加一个bbs,bbs需要配置在iis的根目录,结果配置bbs成功之后,应用起不来了,出现错误提示,分析认为是web.config冲突造成的。 解决方法如下: 修改bbs中...
  • yaoyaoopen
  • yaoyaoopen
  • 2014年04月22日 18:19
  • 1032

C# Web.Config 加密与解密

用后台编码的形式对web.config数据库连接字符串或者某个节点进行加密 这里提供俩种方式: DataProtectionConfigurationProvider和RSAProtectedCo...
  • sat472291519
  • sat472291519
  • 2017年04月27日 16:39
  • 815
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Runtime Web.config / App.config Editing
举报原因:
原因补充:

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