ConfigurationSection自定义配置的使用

1、web.config配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
      <section name="log" type="Xys.PetShop.Framework.Configuration.LogConfigurationSetion,Xys.PetShop.Framework"/>
    </configSections>
  <log>
    <filelog path="c:/file.txt" ></filelog>
  </log>
</configuration>

2、Xys.PetShop.Framework.Configuration.LogConfigurationSetion必须继承System.Configuration.ConfigurationSection,

    public class LogElement:System.Configuration.ConfigurationElement
    {
        [ConfigurationProperty("path")]
        public string Path
        {
            get { return this["path"] as string; }
            set { this["path"] = value; }
        }
    }

    public class LogConfigurationSetion : ConfigurationSection
    {
        [ConfigurationProperty("filelog")]
        public LogElement FileLog
        {
            get { return this["filelog"] as LogElement; }
            set { this["filelog"] = value; }
        }
    }

调用

          LogConfigurationSetion config = (LogConfigurationSetion)ConfigurationManager.GetSection("log");
           System.Console.WriteLine(config.FileLog.Path);

posted @ 2011-09-21 16:20  虎头  阅读(1731)  评论(0编辑  收藏  举报