当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  ASP.NET2.0里web.config配置的读写

 ASP.NET2.0里web.config配置的读写

点击次数:18 次 发布日期:2008-11-26 11:46:54 作者:源代码网
源代码网推荐     
源代码网推荐   在ASP.NET2.0里不但进一步扩展了配置文件web.config,更为重要的是系统提供了一组API函数,让我们可以以编程的方式从配置文件里提取信息
源代码网推荐  
源代码网推荐   首先,先看看如果从web.config里提取appSettings里的配置值,示例代码如下:
源代码网推荐  
源代码网推荐  
源代码网推荐  利用ASP.NET2.0提供的一组API函数,您可以很容易的获取AppSettingsSection里所有的Keys/value组对,如下:
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  Configuration config
源代码网推荐  
源代码网推荐  = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
源代码网推荐  
源代码网推荐  AppSettingsSection appSettings = (AppSettingsSection) config.GetSection("appSettings");
源代码网推荐  
源代码网推荐  string[] appKeys = appSettings.Settings.AllKeys;
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  for (int i = 0; i < appSettings.Settings.Count; i++)
源代码网推荐  
源代码网推荐  ...{
源代码网推荐  
源代码网推荐  //这里只进行简单的输出
源代码网推荐  
源代码网推荐  Response.Write(appSettings.Settings[appKeys[i]].Value);
源代码网推荐  
源代码网推荐  Response.Write("<BR>");
源代码网推荐  
源代码网推荐  }
源代码网推荐  
源代码网推荐  上面代码只是进行简单的输出所有Key的value值,然而,你可能想获取的仅仅是某一个key的值,这也非常简单,如下:
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
源代码网推荐  
源代码网推荐  AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  string pateTitle= appSettings.Settings["pagetitle"].Value; //获取key为patetitle的value值
源代码网推荐  
源代码网推荐  string siteLogo= appSettings.Settings["siteLogo"].Value; //获取key为sitelogo的value值
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  对于数据库连接字符串,在ASP.NET2.0里提供了专门的配置节如下:
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  <connectionStrings>
源代码网推荐  
源代码网推荐   <add name="connectionstring"
源代码网推荐  
源代码网推荐  connectionString="Data Source=SQLEXPRESS;AttachDbFilename=JsskDb.mdf; … .."/>
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  <add name="MyProviderConnectionString"
源代码网推荐  
源代码网推荐  connectionString="Data Source=SQLEXPRESS;Integrated Security=True; … …"/>
源代码网推荐  
源代码网推荐  </connectionStrings>
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  这样我们很容易获取数据库连接字符串如下:
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  Configuration config
源代码网推荐  
源代码网推荐  = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
源代码网推荐  
源代码网推荐   ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection("connectionstring ");
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  ConnectionStringSettingsCollection conCollection = conSection.ConnectionStrings;
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  foreach (ConnectionStringSettings conSetting in conCollection)
源代码网推荐  ...{
源代码网推荐  
源代码网推荐  Response.Write(conSetting.ConnectionString);
源代码网推荐  
源代码网推荐  Response.Write("<BR>");
源代码网推荐  
源代码网推荐  }
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  另外,利用API函数,你同时还可以在代码里更改web.config数据库连接的配置的值,如下
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  ConnectionStringsSection conSection
源代码网推荐  
源代码网推荐   = (ConnectionStringsSection)config.GetSection("connectionStrings");
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  conSection.ConnectionStrings["SQLConnectionString"].ConnectionString =
源代码网推荐  
源代码网推荐  "Data Source=SQLEXPRESS;Integrated Security=True; … …";
源代码网推荐  
源代码网推荐  config.Save();
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  这里最有意思的可能就是类的转换,在<appSettings ></appSettings>里,使用的是AppSettingsSection类,在<connectionStrings></ connectionStrings>里使用的的是ConnectionStringsSection类,事实上,ASP.NET2.0提供的一组函数都是“配置节名+Section”的形式提供的类。
源代码网推荐  
源代码网推荐   在ASP.NET官方网站曾经对此专门介绍,可以找不到该文件了。
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  在ASP.NET2.0里提供了两种方式对数据库连接字符串加密,一种是使用asp_regii命令,一种是通过代码,下面显示的是通过代码方式对数据库连接字符串加密,代码如下:
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
源代码网推荐  
源代码网推荐  ConfigurationSection configSection = config.GetSection("connectionStrings");
源代码网推荐  
源代码网推荐  
源代码网推荐  if (configSection.SectionInformation.IsProtected)
源代码网推荐  
源代码网推荐  ...{//如果已经加密,就不用再加密了
源代码网推荐  
源代码网推荐  configSection.SectionInformation.UnprotectSection();
源代码网推荐  
源代码网推荐  config.Save();
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  }
源代码网推荐  
源代码网推荐  else
源代码网推荐  
源代码网推荐  ...{
源代码网推荐  
源代码网推荐  configSection.SectionInformation.ProtectSection ("DataProtectionConfigurationProvider");
源代码网推荐  
源代码网推荐  config.Save();
源代码网推荐  
源代码网推荐  }
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  这样,你检查该文件的配置可能如下:
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
源代码网推荐  
源代码网推荐  <EncryptedData>
源代码网推荐  
源代码网推荐  <CipherData>
源代码网推荐  
源代码网推荐  <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAVClqG40BZkCjK40
源代码网推荐  
源代码网推荐  adynN8gQAAAACAAAAAAADZgAAqAAAABAAAABIhtOW …PE
源代码网推荐  
源代码网推荐  </CipherData>
源代码网推荐  
源代码网推荐  </EncryptedData>
源代码网推荐  
源代码网推荐  </connectionStrings>
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1487303
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华