首页 > Web开发 > 详细

[转载]C# 中Web.config文件的读取与写入

时间:2014-01-18 01:38:33      阅读:357      评论:0      收藏:0      [点我收藏+]

asp.net2.0新添加了对web.config直接操作的功能。开发的时候有可能用到在web.config里设置配置文件,其实是可以通过程序来设置这些配置节的。

asp.net2.0需要添加引用:

using System.Web.Configuration;

web.config里的配置节:

bubuko.com,布布扣
  <appSettings>
    
<add key="FilePath" value="g:\Test\WebConfigManager\Upload\" />
    
<add key="p" value="g:\" />
  
</appSettings>

(1)读

string filepath = ConfigurationManager.AppSettings["FilePath"];
 
(2)添加
bubuko.com,布布扣
        Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
        AppSettingsSection app 
= config.AppSettings;
        app.Settings.Add(
"p""p:\\");
        config.Save(ConfigurationSaveMode.Modified);
bubuko.com,布布扣
 
(3)修改
bubuko.com,布布扣
          Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
            AppSettingsSection app 
= config.AppSettings;
            app.Settings[
"p"].Value = @"g:\";
            config.Save(ConfigurationSaveMode.Modified);
bubuko.com,布布扣
 
(4)删除
bubuko.com,布布扣
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
        AppSettingsSection app 
= config.AppSettings;
        app.Settings.Remove(
"p");
        config.Save(ConfigurationSaveMode.Modified);
bubuko.com,布布扣
 
 
 
注意:
(1)asp.net用户需要有读取、修改、写入的权限。
(2)WebConfigManager是web.config所在的文件夹名。

[转载]C# 中Web.config文件的读取与写入

原文:http://www.cnblogs.com/iack/p/3524680.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!