1工具介绍
子曾经曰过:工欲善其事,必先利其器(《论语·魏灵公》)。
先来介绍工具——一个全世界.NET编程人员必备的工具——掌声有请Lutz Roeder的Reflector!(热烈的掌声,啪啪啪…….)。软件下载地址:http://www.aisto.com/roeder/dotnet/。
这是一个超级伟大的软件,有了它,整个.NET基础类库就等于是开源的了。
界面截图如下:
图1 Reflector界面
2.配置文件概述
配置文件能使我们开发的软件具有较强的适应性和更大的灵活性,windows下的配置技术有.ini文件、注册表等,现在在.NET开发平台下,推荐使用基于XML的.config配置文件。.config文件分成两大类,一类服务于web程序,文件名:web.config;另一类服务于windows应用程序,文件名:***.exe.config(其中***代表可执行文件的名称)。
下面分别讲授这两种文件的操作。
3.web程序的配置文件
3.1 web程序的配置文件的组成
ASP.NET开发中主要用到的.config文件有两个:machine.config和web.config。其中machine.config对于整个服务器来说,具有全局性的特点,我们一般不去改变其设置。Web.config则对应于每个web程序本身。先看一个典型的web.config文件:
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="message" value="Hello World!" />
</appSettings>
<connectionStrings>
<add name="AdventureWorks" connectionString="..."/>
<add name="pubs" connectionString="..."/>
</connectionStrings>
<system.web>
<compilation debug="true" />
<authentication mode="Windows"/>
<identity impersonate="true"/>
</system.web>
</configuration>
|
这个web.config文件中,我们可以看到三个节(section):appSettings、connectionStrings、system.web。其中appSettings节其实就完成了我们以前用.ini文件的功能。不过它功能更强,可以对其中的元素进行加密(后面讲)。connectionStrings节用于配置数据库连接。
3.2 对配置文件的常见操作
对于配置文件的常见操作包括:
l 读取
l 修改
l 将web.config中的配置节放在单独的文件中
l 对某一节进行加密
l 添加定制的节
操作web配置文件(包括machine.config和web.config等)的命名空间是:System.Web.Configuration。主要应用的类是:WebConfigurationManager。下面看看WebConfigurationManager类的成员。(可以利用MSDN来查看,我下面是利用Lutz Roeder的Reflector)
图 1 WebConfigurationManager类成员
3.2.1 读取
图 2WebConfigurationManager类中用于读取的属性和方法
3.2.1.1 读取appSettings节和connectionStrings节
在WebConfigurationManager类中,我们首先注意到其两个属性AppSettings和ConnectionStrings。这两个属性就是用于操作我们前面看到的web.config文件中的两节appSettings和connectionStrings。下面演示使用方法。
演示操作appSettings节的代码:
using System.Web.Configuration;
....
string message;
message = WebConfigurationManager.AppSettings["message"];
...
|
演示操作connectionStrings节的代码:
using System.Web.Configuration;
....
string connectionString =
WebConfigurationManager.ConnectionStrings["pubs"].ConnectionString;
...
|
3.2.1.2 读取其它节
在.NET中读取其它节要麻烦一些。我们需要利用GetSection()函数。GetSection()函数的作用是从当前web应用程序的配置文件中检索指定的配置节。GetSection()函数有2个重载,我们这里介绍GetSection(string sectionName)。这个函数参数sectionName是用于查找节点的XPath表达式(对于XPath表达式的了解请参阅其它资料)。这里我们首先需要知道的是在.NET中所有配置节都对应一个类,譬如上面我们看到的web.config文件中,appSettings节对应于AppSettingsSection类,而connectionStrings对应于ConnectionStringsSection类。利用Lutz Roeder的Reflector我们可以很容易的知道所有的section类。见下图。
图 3 继承于ConfigurationSection类的各section类
从中我们查找到compilation节对应于System.Web.Configuration. CompilationSection 类,authentication节对应于System.Web.Configuration.AuthenticationSection类,identity节对应于System.Web.Configuration.IdentitySection类,等等。
例子1:
protected void readImpersonationButton_Click(object sender, EventArgs e)
{
IdentitySection section;
section = WebConfigurationManager.GetSection("system.web/identity")
as IdentitySection;
if (section != null)
{
WriteMessage("Impersonate = " + section.Impersonate);
}
}
private void WriteMessage(string message)
{
//在页面上添加一个名为messagePlaceHolder的PlaceHolder控件
HtmlGenericControl generic = new HtmlGenericControl();
generic.InnerHtml = message;
messagePlaceHolder.Controls.Add(generic);
}
|
上面例子中的XPath表达式system.web/identity相信大家都能猜出来是什么意思。而至于IdentitySection类到底有什么成员则需要利用Lutz Roeder的Reflector查找或MSDN查找。
例子2:(MSDN上的例子)此示例演示一个可从 Web 应用程序或控制台应用程序访问的节。
// Show the use of GetSection(string).
// It gets the connectiobStrings section.
// If called from within a client application,
// the GetSection(string) gets the default connectionStrings
// section from the machine.config.
// If called from within a Web aplication it gets the
// section from the configuration file located at the
// application current level.
static void GetSection1()
{
// Get the connectionStrings section.
ConnectionStringsSection connectionStringsSection =
WebConfigurationManager.GetSection("connectionStrings")
as ConnectionStringsSection;
// Get the connectionStrings key,value pairs collection.
ConnectionStringSettingsCollection connectionStrings =
connectionStringsSection.ConnectionStrings;
// Get the collection enumerator.
IEnumerator connectionStringsEnum =
connectionStrings.GetEnumerator();
// Loop through the collection and
// display the connectionStrings key, value pairs.
int i = 0;
Console.WriteLine("[Display the connectionStrings]");
while (connectionStringsEnum.MoveNext())
{
string name = connectionStrings[i].Name;
Console.WriteLine("Name: {0} Value: {1}",
name, connectionStrings[name]);
i += 1;
}
Console.WriteLine();
}
|
在MSDN上还有其它几个例子可以参考。
3.2.2 修改
从图2我们可以看到还有几个静态函数OpenWebConfiguration()、OpenMachineConfiguration()等,就是用来打开一个配置文件(分别对应于web.config和machine.config)修改更新配置项的。
例子:把system.web项下的compilation修改(false->true;或true->false)。
protected void toggleDebugButton_Click(object sender, EventArgs e)
{
Configuration config;
config = WebConfigurationManager.OpenWebConfiguration("~");
CompilationSection compilation;
compilation = config.GetSection("system.web/compilation")
as CompilationSection;
if (compilation != null)
{
compilation.Debug = !compilation.Debug;
config.Save();
WriteMessage("Debug setting is now: " + compilation.Debug);
}
}
修改的注意点:
1. 必须对该文件有修改的权限,通常NETWORK SERVICE和ASPNET账户对文件和目录没有修改权限。
2. ASP.NET在运行时,Runtime时刻注视着web.config文件的变化,一旦该文件修改了,整个应用程序就创建一个新的实例,并重新加载。如果频繁的修改web.config文件,对程序性能影响比较大。
3. 如果需要频繁修改配置,则应把该配置节放到单独的文件中。
3.2.3 将web.config中的配置节放在单独的文件中
我们可以把任意一个配置节(section)放入一个独立的文件中。把web.config中的配置节放在单独的文件中的优点,等一下再说。先看看怎么做。采用本文开始的例子,分解如下。
例子:
web.config文件
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings configSource="appSettings.config"/>
<connectionStrings configSource="connections.config"/>
<system.web>
<compilation debug="true" />
<authentication mode="Windows"/>
<identity impersonate="true"/>
</system.web>
</configuration>
|
appSettings.config文件:
<appSettings>
<add key="message" value="Hello World!"/>
</appSettings>
|
connections.config文件:
<connectionStrings>
<add name="AdventureWorks" connectionString="..."/>
<add name="pubs" connectionString="..."/>
</connectionStrings>
|
利用外部配置文件的好处:
1. 可以根据不同的环境而设置不同的配置,譬如开发环境、测试环境、正式环境配置不同的数据库连接。
2. 权限管理。例如,可以锁住web.config不让某些人修改,而只让其用于修改appSettings.config文件的权限。
3. 利用外部配置文件,还可以控制当其修改后应用程序是否重启。一般,当我们修改了web.config文件时,应用程序就产生一个新的实例并重启。而如果是单独的配置文件,则可以控制重启与否。
配置方法:在machine.config中配置。Machine.config文件一般在C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG目录下。打开machine.config文件,截图如下:
图 4 machine.config文件内容截图
从图4我们可以看到在有的元素中有restartOnExternalChanges属性。如果设为false,则外部文件修改后不重启,如果设为true,则重启。
3.2.4 对某一节进行加密
在.NET2.0中加密某一节(section),那是相当的简单。
在配置文件中,有些信息是不希望别人看到的。例如在<connectionStrings>节中可能包含连接数据库的用户名和密码。<identity>节中也可能包含用户名和密码。
注意:有些节可能含有密码,但不能加密,例如<processModel>节,为了安全,你可以应用Aspnet_setreg.exe工具来保存密码。
|
下面的例子演示如何加密和解密一节。提醒,这里的加密和解密用的单词是protect和unprotect。注意:在实际应用中,我们并不需要明确的调用解密函数。在运行市,程序读取到加密了的配置节时将会自动调用解密。
protected void toggleEncryptionButton_Click(object sender, EventArgs e)
{
Configuration config;
config = WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection section;
section = config.GetSection("connectionStrings")
as ConnectionStringsSection;
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
}
else
{
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
config.Save();
WriteMessage("connections protected = " + section.SectionInformation.IsProtected);
}
|
此代码的作用就是加密和解密connectionStrings节。如果原来加密(通过属性IsProtected判断)了,则解密。否则相反。分别调用了UnprotectSection()和protectSection()函数。
下面是相关文件的变化。
加密前的connectionStrings.config文件内容如下:
图 5 加密前的connectionStrings.config文件内容
加密后connectionStrings.config文件内容如下:
图 6 加密后connectionStrings.config文件内容
对于结果的改变,我们可以对比一下,自己了解其变化。
但在上面的代码片断中,我们还有一个地方需要了解:加密提供者。目前有两种加密提供者:DataProtectionConfigurationProvider 和 RSAProtectedConfigurationProvider。其中DataProtectionConfigurationProvider是利用Windows Data Protection API(DPAPI)提供与机器相关的加密解密。也就是说我们在哪一台机器上加密就只能在那一台机器上解密。如果需要在其它机器上解密则只能使用DataProtectionConfigurationProvider。
3.2.5添加定制的配置节
当我们需要配置的数据比较多的时候,为了管理的方便。我们可能需要添加定制的配置节。下面用一个比较简单的例子来演示。
<configuration>
<configSections>
<section name="Logging" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<Logging>
<add key="File Name" value="MyApp.log" />
<add key="MessageColumns" value="5" />
<add key="MaxFileSize" value="40000000000000" />
</Logging>
</configuration>
|
分享到:
相关推荐
ASP.NET 2.0 读取配置文件[INI]
ASP.NET2.0中配置文件的加密与解密.
在.net编程中,我们经常用到config文件来保存一些常用的应用程序配置信息,在WinForm中这个文件名字是app.config,在asp.net中叫web.config。这个.config文件其实就是一个xml文件,对它的读操作微软已经提供了一个类...
mail发送与接收、文件处理技术、GDI+图形图像、多媒体技术、水晶报表、XML技术、ASP.NET 2.0中的成熟Ajan架构——Atlas、ASP.NET的配置与网站管理工具、ASP.NET应用程序安全性、程序调试与错误处理以及网站发布、...
.NET 2.0获取配置文件AppSettings和ConnectionStrings节数据的方法
ASP.NET2.0配置文件Web.config 详细解释.rar
Asp.Net2.0读取-修改-新增(配置文件web.config)可以是 ***.config里面有3个文件操作类,System.config,使用文档
WebService基础、创建和使用WebSerwice、E—mail发送与接收、文件处理技术、GDI+图形图像、多媒体技术、水晶报表、XMI技术、ASP.NET 2.0中的成熟Ajall架构——Atlas、ASP.NET的配置与网站管理工具、ASP.NET应用程序...
精通ASP.NET2.0的Web2.0+电子教程 本书重点介绍了使用ASP.NET 2.0(C#)技术开发Web 2.0时代的热门网 络应用系统(如RSS、Blog、Tags、Web Servicc、BBS、XML、Ajax、WIKI等 )的方法,以及开发这些系统...
HandlerSample 图片的处理程序以及防止盗链的方法 FileHandlerSample 文件下载示例 <br>第13章(\C13) 示例描述:本章示例展现了在ASP.NET2.0中实现在线服务的功能。 OnlineService 复杂的...
● 简单介绍ASP.NET 2.0 ● 回顾ASP.NET 2.0解决的Internet编程问题 ● 解释ASP.NET 2.0是怎样适应其他技术的 ● 浏览一个由ASP.NET 2.0构建的站点的动态功能 ● 理解创建ASP.NET 2.0页面(ASPX)将要使用的工具—— ...
《ASP.NET2.0(C#)大学实用教程(电子教案)》,个人觉得比较适合初学者 。 它讲解比较透彻系统,全部是ppt格式,适合学习和教学用。 本教案包含了13个ppt文件,每个ppt文档对应的是一个章节,文档名字如下: 第1章 ...
ASP.NET 2.0+SQL Server 2005全程指南 目录 基础篇 第1章 ASP.NET概述及环境配置 1.1 认识ASRNET 1.1.1 .NET Framework框架 1.1.2 ASP.NET功能与特性 1.1.3 ASP.NET与ASP的区别 1.2 搭建ASP.NET开发环境 1.2.1...
AdFile.xml AdRotator配置文件。 7-08.aspx 使用Calendar控件创建日历。 第8章(\Chapter 08) 示例描述:本章学习用户控件和自定义控件。 uc1.ascx 用户控件实现搜索框。 8-01.aspx 用户控件的调用。 uc2...
ASP.NET 2.0数据库通用模块开发与系统移植之多媒体文件
作者:程不功 龙跃进 桌琳 出版社:清华大学出版社 ASP.NET 2.0是微软公司推出的新一代企业级开发...2.2.3 网站配置文件(Web.config) 2.2.4 网站全局文件(Global.asax) 2.2.5 两个共享的目录 等等。。。
(3)将弹出“附加数据库”对话框,在该对话框中单击“ ”按钮,选择所要附加数据库的.mdf文件,单击“确定”按钮,即可完成数据库的附加操作。 配置IIS (1)依次选择“开始”/“设置”/“控制面板”/“管理工具”/...
登录控件的综合应用,数据库附加的是aspnetdb.mdf 微软提供的,主要是配置文件里如何配置....