Runtime Web.config / App.config Editing
2004-09-07 11:14
405 查看
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...
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 th
4000
is 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
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.
<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...
|
4000
is 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. |
相关文章推荐
- Runtime Web.config / App.config Editing
- Runtime Web.config / App.config Editing
- Runtime Web.config / App.config Editing
- web.config/app.config敏感数据加/解密的二种方法
- 创建自定义配置节点(web.config和app.config都适用)
- web.config 或 app的config中自定义节点
- 在web.config和app.config文件中增加自定义配置节点
- 在web.config和app.config文件中增加自定义配置节点
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- Web.Config文件 httpRuntime 限制上传文件大小与时间
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
- Log4net不同版本在app.config和web.config的配置区别
- ASP.NET web.config 中<appSettings>的设定Oracle连线tns
- Net操作配置文件(Web.config|App.config)通用类
- VS web.config/app.conifg配置文件自定义类型使用智能感知功能
- Designing the Application Architecture - changing web.config for azure app fabric settings
- 【转载】App.config/Web.config 中特殊字符的处理
- AppSettings In web.config