但是这个变量不会一个固定的值,会根据实际情况而发生变化,比如在需要读取一个配置文件的路径,而这个路径是站点发布的实际硬盘路径,如果直接是编译时状态,没有问题。但是如果站点iis更换路径,就需要修改这个web.config中的参数。如果能将这个编译时状态修改为运行时状态,那将更为合理和方便。这就需要存在一种在代码中能够动态修改web.config的方案。

  代码

 

复制代码 代码如下:

  /// <summary>

          /// 写入web.config

          /// </summary>

          /// <param name=”item”>appSettings等</param>

          /// <param name=”key”>键</param>

          /// <param name=”value”>值</param>

          public void WriteConfig(string item, string key, string value)

          {

              if (item == “”)

             {

                 item = “appSettings”;

             }

             Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);

             AppSettingsSection appSection = (AppSettingsSection)config.GetSection(item);

             if (appSection.Settings[key] == null)

             {

                 appSection.Settings.Add(key, value);

                 config.Save();

             }

             else

             {

                 appSection.Settings.Remove(key);

                 appSection.Settings.Add(key, value);

                 config.Save();

            }

         }
您可能感兴趣的文章:

  • 详解asp.net配置文件Web.config
  • ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
  • asp.net web.config加密解密方法
  • ASP.NET(C#)应用程序配置文件app.config/web.config的增、删、改操作
  • asp.net Web.config 详细配置说明
  • asp.net 禁用viewstate在web.config里
  • asp.net access web.config denied
  • 认识ASP.NET配置文件Web.config
  • ASP.NET配置文件Web.config用法详解
  • ASP.NET中Web.config文件的层次关系详细介绍
  • ASP.NET webUploader上传大视频文件相关web.config配置
声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。