自定义配置文件的使用(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 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序. 配置文件的根 ...
随机推荐
- OA 权限控制
对于加入删除 初始化password等操作的权限 控制 第一种方法就是在每一个超链接前加 推断 如 <s:if test="#session.user.hasPrivilegeByNa ...
- Android studio SweetAlert for Android
找到个开源项目.github:https://github.com/pedant/sweet-alert-dialog 效果图:https://raw.githubusercontent.com/pe ...
- (原) Jquery 判断移动设备是IOS / Android系统
var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > - ...
- 第五章SignalR的实时高频通讯
第五章SignalR的实时高频通讯 概述:本例子演示了如果创建一个对象与其他浏览器共享实时状态的应用程序.我们要创建的应用程序为“MoveShape”,该MoveShape页面会显示一个Html Di ...
- css画图形
博客: 史上最强大的40多个纯cs图形 问题:看了上面的博客思考简单的三角行是怎么形成的? #triangle-up { width: 0; height: 0; border-left: 50px ...
- 学习java的视频资源(尚学堂)(比较老旧,但是还是挺好用)
本人新手,转入IT,一开始在学校的时候看过尚学堂 马士兵讲过的java基础视频教程,这次深入学习呢,就从百度云盘找了一整套的视频资源.之后越深入的学习呢,发现这些视频资源VeryCD上都发布了,地址 ...
- 一个Socket连接管理池(心跳机制)
一个Socket连接管理池(心跳机制) http://cuisuqiang.iteye.com/blog/1489661
- android中viewPager实现的屏幕左右切换(入门篇)
大多数的APP都可以实现几个屏幕来回的切换, 首先新建两个Activity,内容随意,布局随意.接下来在MainActivity.xml: <RelativeLayout xmlns:andro ...
- Python爬虫实战(1):爬取Drupal论坛帖子列表
1,引言 在<Python即时网络爬虫项目: 内容提取器的定义>一文我们定义了一个通用的python网络爬虫类,期望通过这个项目节省程序员一半以上的时间.本文将用一个实例讲解怎样使用这个爬 ...
- Swift笔记01
变量使用var 来声明,常量是所有let ,变量名没什么限制 中文表情都可以 ,一般还是使用英文. var str = "luoshuai " //swift语句后面不需要; l ...