(1)只需要简单配置单一属性值:

 <configuration>
<configSections>
<!--配置读取的全名称-->
<section name="simple" type="ConfigNode.SimpleSection,ConfigNode"/>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<!--自定义单一数据-->
<simple maxValue="" minValue=""></simple>
</configuration>

要获取在配置文件中自定义的值,此时在我们上面配置的ConfigNode.SimpleSection,ConfigNode,根据这个创建SimpleSection类,在其中写上获取此节点的属性

  public class SimpleSection : ConfigurationSection//必须要继承这个类
{
/// <summary>
/// 实例化配置属性 元素是否必须 默认值
/// </summary>
[ConfigurationProperty("maxValue", IsRequired = false, DefaultValue = Int32.MaxValue)]
public int MaxValue
{
get
{
//配置文件中的节
return (int)base["maxValue"];
}
set
{
base["maxValue"] = value;
}
}
[ConfigurationProperty("minValue", IsRequired = false, DefaultValue = )]
public int MinValue
{
get { return (int)base["minValue"]; }
set { base["minValue"] = value; }
}
}

使用:

 SimpleSection simple = ConfigurationManager.GetSection("simple") as SimpleSection;
int maxValue = simple.MaxValue;
int minValue = simple.MinValue;

(2)在配置节点的头部,我们也需要配置一个属性值的话

 <configSections>
<section name="colors" type="ConfigNode.ColorsSection,ConfigNode" />
</configSections>
<colors type="颜色">
<color id="skyblue" name="天蓝色"/>
</colors>

ColorsSection类:

 public class ColorsSection : ConfigurationSection
{
[ConfigurationProperty("type", IsRequired = true)]
public string Type
{
get
{
return (string)base["type"];
}
set
{
base["type"] = value;
}
}
[ConfigurationProperty("color", IsDefaultCollection = false)]
public ColorSection Color
{
get
{
return (ColorSection)base["color"];
}
set
{
base["color"] = value;
}
} } public class ColorSection : ConfigurationElement
{
[ConfigurationProperty("id", IsRequired = true, IsKey = true)]
public string Id
{
get
{
return (string)base["id"];
}
set
{
base["id"] = value;
}
}
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return (string)base["name"];
}
set
{
base["name"] = value;
}
}
}

使用和第一的相同

(3)配置多个节点:

 configuration>
<configSections>
<!--这里name的名字 必须与创建的类的名字相同-->
<section name="AnimalSection" requirePermission="false" type="ConfigNode.AnimalSection,ConfigNode"/>
</configSections> <system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<!--这里的也是-->
<AnimalSection>
<add cname="小狗" ename="dog" />
<add cname="小猫" ename="cat" />
<add cname="小兔" ename="rabbit" />
</AnimalSection>

AnimalSection类:

 // 所有配置节点都要选择这个基类  ConfigurationSection
public class AnimalSection : ConfigurationSection
{ private static readonly ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(AnimalCollect), null, ConfigurationPropertyOptions.IsDefaultCollection);
[ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
public AnimalCollect ParamCollection
{
get
{
return (AnimalCollect)base[s_property];
}
} }
/// <summary>
/// 自定义一个集合
/// </summary>
[ConfigurationCollection(typeof(Animal))]
public class AnimalCollect : ConfigurationElementCollection
{
// 基本上,所有的方法都只要简单地调用基类的实现就可以了。
public AnimalCollect()
: base(StringComparer.OrdinalIgnoreCase) // 忽略大小写
{ }
// 其实关键就是这个索引器。但它也是调用基类的实现,只是做下类型转就行了。
new public Animal this[string cname]
{
get
{
return (Animal)base.BaseGet(cname);
}
}
// 下面二个方法中抽象类中必须要实现的。
protected override ConfigurationElement CreateNewElement()
{
return new Animal();
} protected override object GetElementKey(ConfigurationElement element)
{
return ((Animal)element).CName;
}
} /// <summary>
/// 集合中的每个元素
/// </summary>
public class Animal : ConfigurationElement
{
[ConfigurationProperty("cname", IsRequired = true)]
public string CName
{
get
{
return this["cname"].ToString();
}
set
{
this["cname"] = value;
}
} [ConfigurationProperty("ename", IsRequired = true)]
public string EName
{
get
{
return this["ename"].ToString();
}
set
{
this["ename"] = value;
}
}
}

使用:

 var custSection = ConfigurationManager.GetSection("AnimalSection") as AnimalSection;
var s = (from kv in custSection.ParamCollection.Cast<Animal>() select kv).ToList();
string str = string.Empty;
foreach (Animal item in s)
{
str += "中文名:" + item.CName + ",英文名:" + item.EName;
}

(4)配置节点下的多集合节点(键值类型)

 <configuration>
<configSections>
<!--这里name的名字 必须与创建的类的名字相同-->
<section name="AnimalSection" requirePermission="false" type="ConfigNode.AnimalSection,ConfigNode"/>
</configSections> <system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<!--这里的也是-->
<AnimalSection>
<fly>
<add name="燕子" value="swallow" />
<add name="天鹅" value="swan" />
</fly>
<fish>
<add name="鲨鱼" value="shark"/>
<add name="金鱼" value="goldfish"/>
</fish>
<mammalia>
<add name="小狗" value="dog" />
<add name="小猫" value="cat" />
<add name="小兔" value="rabbit" />
</mammalia>
</AnimalSection>
</configuration>

AnimalSection类:

  // 所有配置节点都要选择这个基类  ConfigurationSection
public class AnimalSection : ConfigurationSection
{
[ConfigurationProperty("mammalia", IsDefaultCollection = false)]
public NameValueConfigurationCollection Mammalia
{
get
{
return (NameValueConfigurationCollection)base["mammalia"];
}
set
{
base["mammalia"] = value;
}
} [ConfigurationProperty("fly", IsDefaultCollection = false)]
public NameValueConfigurationCollection Fly
{
get
{
return (NameValueConfigurationCollection)base["fly"];
}
set
{
base["fly"] = value;
}
} [ConfigurationProperty("fish", IsDefaultCollection = false)]
public NameValueConfigurationCollection Fish
{
get
{
return (NameValueConfigurationCollection)base["fish"];
}
set
{
base["fish"] = value;
}
} }

使用:

  AnimalSection animal = ConfigurationManager.GetSection("AnimalSection") as AnimalSection;
string str = string.Empty;
foreach (string key in animal.Mammalia.AllKeys)
{
str += "中文名:" + key + ",英文名:" + animal.Mammalia[key].Value;
}

(5)配置节点下的多集合节点(自定义类型)

 <configuration>
<configSections>
<!--这里name的名字 必须与创建的类的名字相同-->
<section name="FamilySection" requirePermission="false" type="ConfigNode.FamilySection,ConfigNode"/>
</configSections> <system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<FamilySection number="">
<myself name="Z" age="" sex="男" />
<familyMember>
<add name="ZR" age="" sex="男" relation="父子" />
<add name="WY" age="" sex="女" relation="母子" />
<add name="ZZ" age="" sex="男" relation="兄弟" />
<add name="ZG" age="" sex="女" relation="兄妹" />
<remove name="ZG" />
</familyMember>
</FamilySection>
</configuration>

FamilySection类:

 using System.Configuration;
public class FamilySection : ConfigurationSection
{
/// <summary>
/// 获取父节点自定义配置的值
/// </summary>
[ConfigurationProperty("number", IsRequired = true)]
public int Number
{
get
{
return (int)base["number"];
}
set
{
base["number"] = value;
}
}
[ConfigurationProperty("myself", IsDefaultCollection = false)]
public MySelfSection MySelf
{
get
{
return (MySelfSection)base["myself"];
}
set
{
base["myself"] = value;
}
} [ConfigurationProperty("familyMember", IsRequired = false)]
[ConfigurationCollection(typeof(FamilyMemberSection), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap, RemoveItemName = "remove")]
public FamilyMember FamilyMember
{
get
{
return (FamilyMember)base["familyMember"];
}
set
{
base["familyMember"] = value;
}
}
} public class MySelfSection : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return (string)base["name"]; }
set { base["name"] = value; }
}
[ConfigurationProperty("age", IsRequired = true)]
public int Age
{
get { return (int)base["age"]; }
set { base["age"] = value; }
}
[ConfigurationProperty("sex", IsRequired = true)]
public string Sex
{
get { return (string)base["sex"]; }
set { base["sex"] = value; }
}
}
public class FamilyMemberSection : MySelfSection
{
[ConfigurationProperty("relation", IsRequired = true)]
public string Relation
{
get { return (string)base["relation"]; }
set { base["relation"] = value; }
}
} public class FamilyMember : ConfigurationElementCollection
{ protected override ConfigurationElement CreateNewElement()
{
return new FamilyMemberSection();
} protected override object GetElementKey(ConfigurationElement element)
{
return ((FamilyMemberSection)element).Name;
} public FamilyMemberSection this[int i]
{
get
{
return (FamilyMemberSection)base.BaseGet(i);
}
} public FamilyMemberSection this[string key]
{
get
{
return (FamilyMemberSection)base.BaseGet(key);
}
}
}

使用:

  FamilySection family = ConfigurationManager.GetSection("FamilySection") as FamilySection;
string number = family.Number.ToString();
string myself = family.MySelf.Name + "-" + family.MySelf.Age + "-" + family.MySelf.Sex;
string str = string.Empty;
foreach (FamilyMemberSection item in family.FamilyMember)
{
str += item.Name + "-" + item.Age + "-" + item.Sex + "-" + item.Relation;
}

(6)对配置多个section分组

 <configuration>
<configSections>
<!--这里name的名字 必须与创建的类的名字相同--> <sectionGroup type="ConfigNode.TestSectionGroup,ConfigNode" name="textgroup">
<section name="score" type="ConfigNode.ScoreSection,ConfigNode" allowDefinition="Everywhere"/>
<section name="project" type="ConfigNode.ProjectSection,ConfigNode" allowDefinition="Everywhere"/>
</sectionGroup>
</configSections> <system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<textgroup>
<score chinese=""></score>
<project name="测试"></project>
</textgroup>

需要单独配置Group的类:

  public class TestSectionGroup : ConfigurationSectionGroup
{
public ProjectSection Project
{
get
{
return (ProjectSection)base.Sections["project"];
} } public ScoreSection Score
{
get
{
return (ScoreSection)base.Sections["score"];
}
}
}

使用:

 //在exe中使用  TestSectionGroup group = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["textgroup"];
//在web程序中使用:续引用System.Web.Configuration;
TestSectionGroup group = (TestSectionGroup)WebConfigurationManager.OpenWebConfiguration("~").SectionGroups["textgroup"]; string name = group.Project.Name;

自定义配置文件的使用(web.config/app.config)的更多相关文章

  1. MVC.Net:读取Web.config/App.config配置

    需要读取Web.config/App.config的配置很简单,首先我们需要将配置写入到<appSettings>中,例如: <appSettings> <add key ...

  2. 读取、添加、删除、修改配置文件 如(Web.config, App.config)

    private Configuration config; public OperateConfig() : this(HttpContext.Current.Request.ApplicationP ...

  3. web.config/app.config敏感数据加/解密的二种方法

    一 建立虚拟目录  http://localhost/EncryptWebConfig,并添加web.config,其中包含数据库连接字符串: <connectionStrings>    ...

  4. C#的配置文件App.config使用总结

    应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是configuration. ...

  5. ASP.NET MVC Filters 4种默认过滤器的使用【附示例】 数据库常见死锁原因及处理 .NET源码中的链表 多线程下C#如何保证线程安全? .net实现支付宝在线支付 彻头彻尾理解单例模式与多线程 App.Config详解及读写操作 判断客户端是iOS还是Android,判断是不是在微信浏览器打开

    ASP.NET MVC Filters 4种默认过滤器的使用[附示例]   过滤器(Filters)的出现使得我们可以在ASP.NET MVC程序里更好的控制浏览器请求过来的URL,不是每个请求都会响 ...

  6. App.Config详解

    App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是c ...

  7. global.asax?app.config?webconfig??

    一.Global.asax 1.global.asax是什么? 一个文本文件,至于它包含写什么内容?顾名思义,global 肯定是掌管一个应用程序(application)的全局性的东西,例如应用程序 ...

  8. App.Config详解及读写操作

    App.Config详解及读写操作   App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而 ...

  9. c# App.Config详解

    c# App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序. 配置文件的根 ...

随机推荐

  1. ORACLE AUTOMATIC STORAGE MANAGEMENT翻译-第二章 ASM instance(1)

    第二章  ASM INSTANCE ASM的类型,例如可以: 10g后ORACLE instance 类型增加了一个ASM种类.参数INSTANCE_TYPE=ASM进行设置. ASM实例启动命令: ...

  2. 【dfs or 最短路】【HDU1224】【Free DIY Tour】

    路径只能由小序号到大序号..(起点可以视为最小的序号和最大的序号) 问怎么走 happy值最大.. DFS N=100 且只能小序号到大序号 显然dfs可以过.. 但是存路径的时候sb了.....应该 ...

  3. Web.config配置和节点介绍

    Web.config文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中 ...

  4. Android开发环境的搭建之(五)DDMS的测试

    启动DDMS(Dalvik Debug Monitor Service) 测试Voice通话功能 左图Device选择MyPhone[Emulator- 5554],右图选择Emulator Cont ...

  5. 用JS做图片轮播

    脚本之家 首页应用手游攻略教程 ﹤首页 >> 网络编程 >> JavaScript >> 网页特效 >> 图象特效 js 图片轮播(5张图片) 作者:m ...

  6. Oracle Semaphore Management in UNIX Administration

    Oracle UNIX/Linux Tips by Burleson Consulting Semaphores and Oracle 11g 信号量和数据库 Semaphores are data ...

  7. Windows下的窗口程序

    再进一步. . .model flat,stdcall option casemap:none include Windows.inc include user32.inc includelib us ...

  8. sql的集合运算

    表的加减法 union:使用union 对表进行假发(并集)运算, union等集合运算符通常都会去除重复记录. select shohin_id, shohin_mei from shohin un ...

  9. PHP的五种常见设计模式

    工厂模式 最初在设计模式 一书中,许多设计模式都鼓励使用松散耦合.要理解这个概念,让我们最好谈一下许多开发人员从事大型系统的艰苦历程.在更改一个代码片段时,就会发生问题,系统其他部分 —— 您曾认为完 ...

  10. ubuntu mysql编码设置5.5以后

    安装了mysql. 版本:5.5.28 操作系统 :ubuntu 12.10 mysql> show variables like 'character%'; +---------------- ...