现在的位置: 首页 > 综合 > 正文

自定义.NET应用程序配置节实例

2013年08月26日 ⁄ 综合 ⁄ 共 4481字 ⁄ 字号 评论关闭

在.NET中,app.config和web.config是应用程序的配置文件。我们可以自定义配置节来实现读取和写入完全自定义的配置信息,使配置信息更一目了然。

将一系列相关的配置保存在配置文件中,可以定义configSections,可以使用NameValueSectionHandler、DictionarySectionHandler来处理集合,但是在写入的时候比较麻烦(如果谁知道比较简便的办法,烦请告诉我,谢谢),而且在有的地方需要比较复杂的配置的时候,还是自定义配置来得容易。

配置文件中的元素称为基本 XML 元素或节(Section)。基本元素只是具有相关属性(如果有)的简单 XML 标记。节最简单的形式与基本元素一致。而复杂的节可以包括一个或多个基本元素、元素的集合以及其他节。 

可以用ConfigurationSection和IConfigurationSectionHandler来扩展自定义配置,但后者已经被2.0框架否决。因此,我在这里使用ConfigurationSection来实现一个自定义配置。

ConfigurationElement 用作表示 XML 配置元素(如 ConfigurationSection)的类的基类。可以扩展 ConfigurationElement 类,以使其表示 ConfigurationSection 节中的配置元素。也可以创建一个 ConfigurationElement 元素的 ConfigurationElementCollection 集合。

配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<configSections>
    
<section name="fileTypes" type="CustomConfig.ConfigSection, CustomConfig"/>
  
</configSections>

  
<fileTypes>
    
<files>
      
<clear />
      
<add file="htm" desc="html page" />
      
<add file="html" desc="html page" />
      
<add file="asp" desc="Active Server Page file" />
    
</files>
  
</fileTypes>

</configuration>

我们需要3个类来实现这个自定义配置。

继承自ConfigurationSection的类管理配置节,因为要保存集合,所以要扩展ConfigurationElement和ConfigurationElementCollection两个类,下面看代码:

ConfigHandler.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace CustomConfig
{
    
public class ConfigSection: ConfigurationSection
    
{
        ConfigElement element;

        
public ConfigSection()
        
{
            element 
= new ConfigElement();
        }


        [ConfigurationProperty(
"files")]
        
public ConfigElementCollection FileTypes
        
{
            
get
            
{
                ConfigElementCollection types 
= (ConfigElementCollection)base["files"];
                
return types;
            }

        }


        
protected override void DeserializeSection(System.Xml.XmlReader reader)
        
{
            
base.DeserializeSection(reader);
        }


        
protected override string SerializeSection(ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
        
{
            
return base.SerializeSection(parentElement, name, saveMode);
        }

    }

}

 

ConfigElement.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace CustomConfig
{
    
public class ConfigElement : ConfigurationElement
    
{
        
public ConfigElement()
        
{
        }


        
public ConfigElement(string fileType)
        
{
            FileType 
= fileType;
        }


        
public ConfigElement(string fileType, string description)
        
{
            FileType 
= fileType;
            Description 
= description;
        }


        [ConfigurationProperty(
"file")]
        
public string FileType
        
{
            
get return (string)this["file"]; }
            
set this["file"= value; }
        }


        [ConfigurationProperty(
"desc")]
        
public string Description
        
{
            
get return (string)this["desc"]; }
            
set this["desc"= value; }
        }


        
protected override void DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey)
        
{
            
base.DeserializeElement(reader, serializeCollectionKey);
        }


        
protected override bool SerializeElement(System.Xml.XmlWriter writer, bool serializeCollectionKey)
        
{
            
return base.SerializeElement(writer, serializeCollectionKey);
        }


        
protected override bool IsModified()
        
{
            
return base.IsModified();
        }

    }

}

 

ConfigElementCollection.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace CustomConfig
{
    
public class ConfigElementCollection : ConfigurationElementCollection
    
{
        
public ConfigElementCollection()
        
{
            ConfigElement element 
= (ConfigElement)CreateNewElement();
            Add(element);
        }


        
protected override ConfigurationElement CreateNewElement()
        
{
            
return new ConfigElement();
        }


        
protected override object GetElementKey(ConfigurationElement element)
        
{
            
return ((ConfigElement)element).FileType;
        }


        
public override ConfigurationElementCollectionType CollectionType
        
{
            
get
            
{
                
//return base.CollectionType;
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }

        }


        
public new string AddElementName
        
{
            
get 

抱歉!评论已关闭.