自定义配置文件的使用(web.config/app.config)
(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)的更多相关文章
- MVC.Net:读取Web.config/App.config配置
需要读取Web.config/App.config的配置很简单,首先我们需要将配置写入到<appSettings>中,例如: <appSettings> <add key ...
- 读取、添加、删除、修改配置文件 如(Web.config, App.config)
private Configuration config; public OperateConfig() : this(HttpContext.Current.Request.ApplicationP ...
- web.config/app.config敏感数据加/解密的二种方法
一 建立虚拟目录 http://localhost/EncryptWebConfig,并添加web.config,其中包含数据库连接字符串: <connectionStrings> ...
- C#的配置文件App.config使用总结
应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是configuration. ...
- ASP.NET MVC Filters 4种默认过滤器的使用【附示例】 数据库常见死锁原因及处理 .NET源码中的链表 多线程下C#如何保证线程安全? .net实现支付宝在线支付 彻头彻尾理解单例模式与多线程 App.Config详解及读写操作 判断客户端是iOS还是Android,判断是不是在微信浏览器打开
ASP.NET MVC Filters 4种默认过滤器的使用[附示例] 过滤器(Filters)的出现使得我们可以在ASP.NET MVC程序里更好的控制浏览器请求过来的URL,不是每个请求都会响 ...
- App.Config详解
App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是c ...
- global.asax?app.config?webconfig??
一.Global.asax 1.global.asax是什么? 一个文本文件,至于它包含写什么内容?顾名思义,global 肯定是掌管一个应用程序(application)的全局性的东西,例如应用程序 ...
- App.Config详解及读写操作
App.Config详解及读写操作 App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而 ...
- c# App.Config详解
c# App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序. 配置文件的根 ...
随机推荐
- 关于视觉里程计以及VI融合的相关研究(长期更新)
1. svo 源码:https://github.com/uzh-rpg/rpg_svo 国内对齐文章源码的研究: (1)冯斌: 对其代码重写 https://github.com/yueying/O ...
- MVC模式下My97DatePicker日期控件引用注意事项
My97DatePicker日期控件之前在用webform模式开发的时候,只要 <script language="javascript" type="text/j ...
- rem和em和px vh vw和% 移动端长度单位
1.rem和em.px 首先来说说em和px的关系 em是指字体高度 浏览器默认1em=16px,所以0.75em=12px;我们经常会在页面上看到根元素写的font-size:65%; 这样em就成 ...
- iOS集成微信支付
微信支付的开发 前言:之前听说过微信支付有很多坑,其实没有想象的那么坑,整体感觉很容易上手,按照它的流程来不会有错!PS:官方的流程看的TMD烦,好啦,废话有点多,进入开发.(ps:每个微信的版本一直 ...
- panel控件 换行
Panel1.Controls.Add(new LiteralControl("<BR/>"));
- zoj1025 Wooden Sticks
DAG转移,从切题的数量来看是一道水题,给你n个棒,大的可以延续小的,问最少上升子序列的个数. 其实这道题是用贪心来写的,因为这是个有向无环图,到达分叉口,每一条路都要便历,所以每条路应该一样对待,有 ...
- oracle distinct 去除重复,同时按某字段排序
SELECT distinct supplier_id, min(evidence_date) as evidence_date FROM TD_SUPPLIER_EVIDENCE_INFO wher ...
- Android onSaveInstanceState()
我们知道,由于手机的内存问题,很容易造成切换activity之后上一个activity被回收的情况,虽然我们按下back按键的时候,还是能够回到上一个activity,但是此时我们并不是执行的onRe ...
- 互联网大公司的CEO,多是程序员出身
互联网有个现象,大公司的CEO,多是程序员出身.举例如下:------马化腾93年深大计算机系毕业,进入润迅通信从软件工程师做到开发部主管,98年11月与张志东等凑齐50万元注册腾讯公司,99年2月开 ...
- Android 自定义EditText实现粘贴,复制,剪切的监听
package com.dwtedx.qq.view; import android.annotation.SuppressLint; import android.content.Context; ...