.Net 自定义读取自定Config文件
2014-12-03 13:29
344 查看
.Net里面,ConfigurationManager默认读取的是Web.config或者App.config但是,什么都放在这两个文件里面,感觉太多了,也不好管理配置。于是参考了下别人的资料,自己写了一个例子,例子实现的的是E:\App.config的文件,文件格式如下
要实现这样的功能,就要实现自己的ConfigurationSection,ConfigurationElementCollection,ConfigurationElement
1.ConfigurationSection
2.ConfigurationElementCollection
3.ConfigurationElement
4.0Config文件
5.0读取
123
请按任意键继续. . .
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="books" type="ConfigurationDemo.BooksSection, ConfigurationDemo"/> </configSections> <books> <book name="123" author="456"/> </books> </configuration>
要实现这样的功能,就要实现自己的ConfigurationSection,ConfigurationElementCollection,ConfigurationElement
1.ConfigurationSection
public class BooksSection : ConfigurationSection { [ConfigurationProperty("books", IsRequired = true)] public string Category { get { return (string)base["Category"]; } set { base["Category"] = value; } } [ConfigurationProperty("", IsDefaultCollection = true)] public BookElementCollection Books { get { return (BookElementCollection)base[""]; } } }
2.ConfigurationElementCollection
public class BookElementCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new BookElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((BookElement)element).Name; } public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } } protected override string ElementName { get { return "book"; } } public BookElement this[int index] { get { return (BookElement)BaseGet(index); } set { if (BaseGet(index) != null) { BaseRemoveAt(index); } BaseAdd(index, value); } } }
3.ConfigurationElement
public class BookElement : ConfigurationElement { [ConfigurationProperty("name", IsRequired = true)] public string Name { get { return (string)base["name"]; } set { base["name"] = value; } } [ConfigurationProperty("author", IsRequired = true)] public double Author { get { return (double)base["author"]; } set { base["author"] = value; } } }
4.0Config文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="books" type="ConfigurationDemo.BooksSection, ConfigurationDemo"/> </configSections> <books> <book name="123" author="456"/> </books> </configuration>
5.0读取
static void Main(string[] args) { string configPath = @"E:\App.config"; ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = configPath; var configManager = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); if (configManager.HasFile) { BooksSection config = (BooksSection)configManager.GetSection("books"); Console.WriteLine(config.Books[0].Name); } }结果
123
请按任意键继续. . .
相关文章推荐
- .net 中读取自定义Config文件
- .net 中读取自定义Config文件
- .net 中读取自定义Config文件
- ASP.NET读取自定义的config文件
- 读取app.config文件中的自定义内容
- C#中读取自定义的config文件(数据库的连接符大多情况使用)
- C# config配置文件 自定义节点读取
- 一种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)
- 在.net中读写config文件的各种方法(自定义config节点)
- 种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)(转自:http://blog.csdn.net/shaily/article)
- 读取自定义的config文件
- 一种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)
- C# WinForm中如何自定义config文件(XML文件),并且读取和保存它
- .net如何自定义config配置文件节点
- .net 如何在web.config中读取自定义节点以及注意的问题(转)
- 种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)
- .NET自定义应用程序配置文件说明app.config
- 在.net中读写config文件的各种方法(自定义config节点)
- 读取自定义的config文件
- Asp.net 自定义config文件读取