NET2.0的配置文件

自定义配置文件的名称,不需要exe.config。

具体的实现代码如下

ExeConfigurationFileMap ^filemap = gcnew ExeConfigurationFileMap;
             filemap
->ExeConfigFilename = "1.xml";
             
// Open App.Config of executable
             System::Configuration::Configuration  ^config = 
             ConfigurationManager::OpenMappedExeConfiguration(filemap,ConfigurationUserLevel::None);

 

然后就可以在dll中使用配置文件的相关类在1.xml进行相关操作了。

随后,对NET2.0的配置方面的内容作了一下总结,贴出如下:

1 section 的类型
ConfigurationSection类   负责普通section
ConnectionStringsSection类 负责数据库连接字符串的特殊section

2 加入一个自定义的section 需要派生ConfigurationSection类
只有ConfigurationSection的子类,才能方便的在该section中读取配置信息。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    
<configSections>
        
<section name="CustomSection" type="testConfiguration.CustomSection, testConfiguration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" requirePermission="true" />
    
</configSections>
    
<CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:05:00" />
</configuration>

以下的section就是派生ConfigurationSection的子类创建的

<CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:05:00" />

上述例子的msdn代码:
自定义ConfigurationSection子类
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref4/html/T_System_Configuration_ConfigurationSection.htm
利用ConfigurationSection子类添加自定义section
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref4/html/C_System_Configuration_ConfigurationSection_ctor.htm

 

3 ConfigurationSection中的属性

仔细观察ConfigurationSection类就会发现
ConfigurationSection类有item属性,但是protected的。目的是为了子类利用该属性访问相应的值 ,如

[LongValidator(MinValue = 1, MaxValue = 1000000,
             ExcludeRange 
= false)]
        
public long MaxUsers
{
            
get
{
                
return (long)this["maxUsers"];
             }

            
set
{
                
this["maxUsers"= value;
             }

 }

MaxUsers是ConfigurationSection子类的属性之一。而item属性不作为外部访问的方法。外部访问通过属性MaxUers访问item属性。

由ConfigurationSection子类的实现可以看到

[LongValidator(MinValue = 1, MaxValue = 1000000,ExcludeRange = false)]
LongValidatorAttribute 是配置属性执行长整型验证,只是针对配置文件中属性的约束。
类似的配置文件属性约束还有:
TimeSpanValidator   StringValidator 如:

 [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'"|", MinLength = 1, MaxLength = 60)]
        public string FileName{}
         [TimeSpanValidator(MinValueString 
= "0:0:30", MaxValueString = "5:00:0", ExcludeRange = false)]
        
public TimeSpan MaxIdleTime{}

4 最后总结了一下所有的有关配置文件操作的相关类

*************************************************
ConfigurationManager 类     生成Configuration 的管理类
Configuration 类     只有通过它才能操作(读取,保存)配置文件,没有构造函数,由ConfigurationManager创建
       Configuration::Sections属性可以读取某个Section。Configuration::Save方法可以保存配置文件


******************Section************************
ConfigurationSectionCollection 类   是Configuration::Sections属性的类型,表示配置文件中相关Section的集合
       ConfigurationSectionCollection[" "]读取某个Section的内容,ConfigurationSectionCollection::Add可以添加一个Section
ConfigurationSection 类     是ConfigurationSectionCollection中具体的元素,表示配置文件中的Section。ConfigurationSection对象代表的Section形如:
       <configSections></configSections>。如果想要自定义一个Section,需要派生ConfigurationSection子类。
       每个Section都包含属性和元素

ConfigurationSectionGroup 类    表示配置文件中相关Section的一种分组方式
ConfigurationSectionGroupCollection 类   表示 ConfigurationSectionGroup 对象的集合

******************Element************************
ConfigurationElement 类     Section下的元素。ConfigurationSection::item属性的类型,利用该属性可以访问一个Section的所有元素
       ConfigurationElement::item属性可以访问该元素的属性 (Property)、特性 (Attribute) 或子元素,但不是外部接口
ConfigurationElementCollection 类   ConfigurationElement 的集合
ConfigurationElementProperty 类    指定配置元素的属性。ConfigurationElementProperty::Validator 属性得到ConfigurationValidatorBase 对象

******************Element的属性******************
ConfigurationProperty 类    表示属性或配置元素的子元素
ConfigurationPropertyAttribute 类   以声明方式指示 .NET Framework,以实例化配置属性
ConfigurationCollectionAttribute 类   以声明的方式指示 .NET Framework 实例化配置元素集合


******************Section的Lock元素**************
ConfigurationLockCollection 类    包含锁定的配置对象的集合。
       对于配置节的锁定属性 (Attribute),存在一个 ConfigurationLockCollection 集合,该集合通过 ConfigurationElement 类的 LockAttributes 属性 (Property) 进行访问。
       对于配置节的锁定元素,存在另一个 ConfigurationLockCollection 集合,该集合通过 ConfigurationElement 类的 LockElements 属性进行访问。

******************Section的Location元素**********
ConfigurationLocationCollection 类   配置文件中的 location 元素。由Configuration::Location属性指定
ConfigurationLocation 类


******************配置文件中字符串的转换*********
ConfigurationConverterBase 类    是转换器配置类型的基类。这些类型用于转换在配置文件中找到的字符串,可将它们转换成相关的强类型属性,也可将它们从强类型属性转换成其他类型。
及所有子类


******************为配置文件定义配置文件映射*****
ConfigurationFileMap 类     为计算机配置文件定义配置文件映射
ExeConfigurationFileMap 子类     为exe、dll程序配置文件定义配置文件映射。
WebConfigurationFileMap 子类    为web程序配置文件定义配置文件映射


******************异常***************************
ConfigurationException 类    发生配置系统错误时引发的异常。常在try catch结构中使用
ConfigurationErrorsException 类    ConfigurationException 的派生类。


******************权限设置***********************
ConfigurationPermission 类    提供允许方法或类访问配置文件的权限结构
ConfigurationPermissionAttribute 类   创建一个 ConfigurationPermission 对象,该对象授予或拒绝访问配置文件节的标记了的目标权限

******************验证***************************
ConfigurationValidatorBase 类    作为基类,用于派生验证类,以便验证对象的值
所有子类      各种类型的元素的验证
ConfigurationValidatorAttribute 类   用作 System.Configuration 验证程序属性类型的基类
所有子类      各种类型的属性的验证

 

ConfigurationSettings 类    提供运行时对读取配置Section和公共配置设置的支持


ConfigurationSet 类     类表示一个或多个 ADAM 实例的配置集

posted @ 2008-03-03 13:17  靓点博客  阅读(171)  评论(0编辑  收藏  举报