c#配置文件的值读取和配置

View Code
public class StartupFoldersConfigSection : ConfigurationSection
{
    [ConfigurationProperty("Folders")]
    public FoldersCollection FolderItems
    {
        get { return ((FoldersCollection)(base["Folders"])); }
    }
}
[ConfigurationCollection(typeof(FolderElement))]
public class FoldersCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new FolderElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {

        return ((FolderElement)(element)).FolderType;

    } 
    public FolderElement this[int idx]
    {
        get
        {
            return (FolderElement)BaseGet(idx);
        }

    }

}
public class FolderElement : ConfigurationElement
{
  
    [ConfigurationProperty("folderType", DefaultValue = "", IsKey = true, IsRequired = true)]
    public string FolderType
    {
        get
        {
            return ((string)(base["folderType"]));
        }
        set
        {
            base["folderType"] = value;
        }
    }
    [ConfigurationProperty("path", DefaultValue = "", IsKey = false, IsRequired = false)]
    public string Path
    {
        get
        {
            return ((string)(base["path"]));
        }
        set
        {
            base["path"] = value;
        }

    }
    [ConfigurationProperty("name",DefaultValue="",IsRequired=false)]
    public string Name
    {
        get { return (string)(base["name"]).ToString(); }
        set { base["name"] = value; }
    }
}
<configuration>
 
  <configSections>
    <section name="StartupFolders" type="StartupFoldersConfigSection"/>
    
  </configSections>
<StartupFolders>
    <Folders>
      <add folderType="A" path="c:\foo" name="aaaaaaaaaaaaaaaa">   
      </add>
      <add folderType="B" path="c:\foo1" name="cccccccccccccccc">
      </add>
    </Folders>
  </StartupFolders>
</configuration>
 StartupFoldersConfigSection section = (StartupFoldersConfigSection)ConfigurationManager.GetSection("StartupFolders");

 Response.Write("<script>alert('" + section.FolderItems[0].FolderType + "')</script>");

 

posted on 2012-10-24 15:36  R.Ray  阅读(300)  评论(0)    收藏  举报

导航