ASP.NET2.0怎么读写web.config配置文件
2008-11-25 20:56
771 查看
在ASP.NET2.0里不但进一步扩展了配置文件web.config,更为重要的是系统提供了一组API函数,让我们可以以编程的方式从配置文件里提取信息
原文地址:http://www.52bcnet.com/show.aspx?id=8034BFBC49F09546
首先,先看看如果从web.config里提取appSettings里的配置值,示例代码如下:
key="pagetitle" value="Job Site Starter Kit (Ver.1.0)">
key="sitelogo" value="logo.gif">
key="advertiseemail" value="sales@somesite.com">
利用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 j = 0; j < appSettings.Settings.Count; j++)
{
//这里只进行简单的输出
Response.Write(appSettings.Settings[appKeys[j]].Value);
Response.Write("");
}
上面代码只是进行简单的输出所有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里提供了专门的配置节如下:
name="connectionstring"
connectionString="Data Source=SQLEXPRESS;AttachDbFilename=JsskDb.mdf; … .."/>
name="MyProviderConnectionString"
connectionString="Data Source=SQLEXPRESS;Integrated Security=True; … …"/>
这样我们很容易获取数据库连接字符串如下:
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("");
}
另外,利用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();
这里最有意思的可能就是类的转换,在里,使用的是AppSettingsSection类,在 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();
}
修改appSettings中某个键值的方法:
public static 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();
}
}
原文地址:http://www.52bcnet.com/show.aspx?id=8034BFBC49F09546
首先,先看看如果从web.config里提取appSettings里的配置值,示例代码如下:
key="pagetitle" value="Job Site Starter Kit (Ver.1.0)">
key="sitelogo" value="logo.gif">
key="advertiseemail" value="sales@somesite.com">
利用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 j = 0; j < appSettings.Settings.Count; j++)
{
//这里只进行简单的输出
Response.Write(appSettings.Settings[appKeys[j]].Value);
Response.Write("");
}
上面代码只是进行简单的输出所有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里提供了专门的配置节如下:
name="connectionstring"
connectionString="Data Source=SQLEXPRESS;AttachDbFilename=JsskDb.mdf; … .."/>
name="MyProviderConnectionString"
connectionString="Data Source=SQLEXPRESS;Integrated Security=True; … …"/>
这样我们很容易获取数据库连接字符串如下:
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("");
}
另外,利用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();
这里最有意思的可能就是类的转换,在里,使用的是AppSettingsSection类,在 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();
}
修改appSettings中某个键值的方法:
public static 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();
}
}
相关文章推荐
- 两种读写配置文件的方案(app.config与web.config通用)
- 在Asp.Net2.0中可以方便的访问配置文件web.config,如判断debug设置,验证类型等。
- asp.net2.0WebConfig的配置文件中设置数据库链接
- asp.net2.0中Web.config文件标准配置简要说明
- asp.net 读写配置文件Web.Config
- ASP.NET2.0里web.config配置的读写
- 在Asp.Net2.0中可以方便的访问配置文件web.config,如判断debug设置,验证类型等
- 实现快速读写配置文件的内容,可以用于读取*.exe.config文件或者Web.Config文件的内容,或者可以读取指定文件的配置项.
- ASP.NET2.0里web.config配置的读写
- ASP.NET2.0里web.config配置的读写
- asp.net2.0中Web.config文件标准配置简要说明
- asp.net2.0中Web.config文件标准配置简要说明
- 在Asp.Net2.0中可以方便的访问配置文件web.config
- ASP.Net2.0使用Log4Net(二)使用单独的配置文件,不放在web.config
- asp.net2.0中Web.config文件标准配置简要说明
- 在Asp.Net2.0中可以方便的访问配置文件web.config,如判断debug设置,验证类型等。
- Web.config配置文件的加密,解密及读写操作
- ASP.NET2.0里web.config配置的读写
- asp.net2.0中Web.config文件标准配置简要说明
- asp.net2.0中Web.config文件标准配置简要说明