refer : http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html

上面这篇写很好了.

在做项目时,我们经常会遇到一些资料,我们不知道该把它们安置在何处.

比如公司资料

1.放在数据库 (资料不经常修改,没必要这么大费周章吧).

2.放在代码里头 (也不是说绝对不会改丫,写在代码里变成dll后不容易修改了耶 /.\)

大部分人都会把这样一群资料统统塞进 webConfig appSettings 里头

数据少的话,其实也没什么,只是一但数据多起来,我们就得分类来装置了。

这里我会教大家如果在 webConfig 中写自己的 Element 来不存资料,而不完全的塞在 appSettings 里头

从 appSettings 或者资料

<appSettings>
<add key="stringData" value="value" />
</appSettings> string value = ConfigurationManager.AppSettings["stringData"].ToString();

自定义一个 object

<configuration>
<configSections>
<section name="objectSection" type="Project.Config.objectSection" /> <!--要在 configSections 内注册哦-->
</configSections>  
<objectSection
stringData="value"
intData=""
/>
</configuration> //对应的 Class
//每一个属性一定要用 getter 来获取
public class objectSection : ConfigurationSection
{
[ConfigurationProperty("stringData", IsRequired = true)]
public string stringData
{
get { return this["stringData"].ToString(); }
} [ConfigurationProperty("intData", IsRequired = true)]
public int intData
{
get { return Convert.ToInt32(this["intData"]); } //这里可以做类型强转等等
}
}

嵌套 object

<configuration>
<configSections>
<section name="objectASection" type="Project.Config.objectASection" />
</configSections>
<objectASection>
<objectBSection
stringData="value"
intData=""
/>
</objectASection>
</configuration> public class objectASection : ConfigurationSection
{
[ConfigurationProperty("objectBSection", IsRequired = true)]
public objectBSection objectBSection
{
get { return (objectBSection)this["objectBSection"]; }
}
}
public class objectBSection : ConfigurationElement //子层继承 Element
{
[ConfigurationProperty("stringData", IsRequired = true)]
public string stringData
{
get { return this["stringData"].ToString(); } //这里可以做类型强转等等
} [ConfigurationProperty("intData", IsRequired = true)]
public int intData
{
get { return Convert.ToInt32(this["intData"]); } //这里可以做类型强转等等
}
}

嵌套 array + object + property 综合版

<configuration>
<configSections>
<section name="objectASection" type="Project.Config.objectASection" />
</configSections>
<objectASection stringData="value">
<objectBSection
stringData="value"
intData=""
/>
<objectCs>
<objectC Id="" stringData="x" />
<objectC Id="" stringData="y" />
<objectC Id="" stringData="z" />
</objectCs>
</objectASection>
</configuration> public class objectASection : ConfigurationSection
{
//normal value
[ConfigurationProperty("stringData", IsRequired = true)]
public string stringData
{
get { return this["stringData"].ToString(); } //这里可以做类型强转等等
} //object value
[ConfigurationProperty("objectBSection", IsRequired = true)]
public objectBSection objectBSection
{
get { return (objectBSection)this["objectBSection"]; }
} //array value
[ConfigurationProperty("objectCs", IsRequired = true)]
public objectCs objectCs
{
get { return (objectCs)this["objectCs"]; }
}
} [ConfigurationCollection(typeof(objectC), AddItemName = "objectC", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class objectCs : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new objectC();
} protected override object GetElementKey(ConfigurationElement element)
{
if (element == null) throw new ArgumentNullException("element");
return ((objectC)element).Id;
}
} public class objectC : ConfigurationElement
{
[ConfigurationProperty("Id", IsRequired = true, IsKey = true)]
public int Id
{
get { return Convert.ToInt32(this["Id"]); }
} [ConfigurationProperty("stringData", IsRequired = true)]
public string stringData
{
get { return this["stringData"].ToString(); }
}
} public class objectBSection : ConfigurationElement //子层继承 Element
{
[ConfigurationProperty("stringData", IsRequired = true)]
public string stringData
{
get { return this["stringData"].ToString(); } //这里可以做类型强转等等
} [ConfigurationProperty("intData", IsRequired = true)]
public int intData
{
get { return Convert.ToInt32(this["intData"]); } //这里可以做类型强转等等
}
} objectASection objectASection = (objectASection)ConfigurationManager.GetSection("objectASection");
string stringData = objectASection.stringData;
stringData = objectASection.objectBSection.stringData; //value
int intData = objectASection.objectBSection.intData; //
List<objectC> objectCs = objectASection.objectCs.Cast<objectC>().ToList();
foreach (objectC objectC in objectCs)
{
int Id = objectC.Id;
stringData = objectC.stringData;
}

还有一种就是类似 appSettings 那样本身就直接是 array 的情况

<configuration>
<configSections>
<section name="BusinessConfig" type="Project.Config.KeyValueConfig" /> <!--可以用同一个class-->
<section name="AbcConfig" type="Project.Config.KeyValueConfig" />
</configSections> <BusinessConfig>
<add key="aa" value=""></add>
<add key="bb" value=""></add>
</BusinessConfig>
<AbcConfig>
<add key="ha" value="a"></add>
<add key="ta" value="b"></add>
</AbcConfig>
</configuration>
namespace Project.Config
{
public class KeyValueConfig : ConfigurationSection
{
private static readonly ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(MyKeyValueCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
[ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
public MyKeyValueCollection KeyValues
{
get { return (MyKeyValueCollection)base[s_property]; }
} }
[ConfigurationCollection(typeof(MyKeyValueSetting))]
public class MyKeyValueCollection : ConfigurationElementCollection
{
public MyKeyValueCollection() : base(StringComparer.OrdinalIgnoreCase) { }
new public MyKeyValueSetting this[string name]
{
get { return (MyKeyValueSetting)base.BaseGet(name); }
} protected override ConfigurationElement CreateNewElement()
{
return new MyKeyValueSetting();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((MyKeyValueSetting)element).Key;
}
}
public class MyKeyValueSetting : ConfigurationElement
{
[ConfigurationProperty("key", IsRequired = true)]
public string Key
{
get { return this["key"].ToString(); }
set { this["key"] = value; }
} [ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return this["value"].ToString(); }
set { this["value"] = value; }
}
}
}
KeyValueConfig mySection = (KeyValueConfig)ConfigurationManager.GetSection("BusinessConfig");
string data = string.Join("\r\n",
(from kv in mySection.KeyValues.Cast<MyKeyValueSetting>()
let s = string.Format("{0}={1}", kv.Key, kv.Value)
select s).ToArray());

ASP.NET Web.Config 读资料 (学习笔记)的更多相关文章

  1. ASP.NET Web.config学习

    花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...

  2. ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法

    ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法 第一种情况,本地开发时,使用本地数据库,如下面的代码 <connectionStrings& ...

  3. Asp.net Web.Config - 配置元素 caching

    Asp.net Web.Config - 配置元素 caching 记得之前在写缓存DEMO的时候,好像配置过这个元素,好像这个元素还有点常用. 一.caching元素列表   元素 说明 cache ...

  4. 【笔记目录2】【jessetalk 】ASP.NET Core快速入门_学习笔记汇总

    当前标签: ASP.NET Core快速入门 共2页: 上一页 1 2  任务27:Middleware管道介绍 GASA 2019-02-12 20:07 阅读:15 评论:0 任务26:dotne ...

  5. asp.net web.config的学习笔记

    原文地址:http://www.cnblogs.com/Bulid-For-NET/archive/2013/01/11/2856632.html 一直都对web.config不太清楚.这几天趁着项目 ...

  6. ASP.NET Web.config文件的配置(Configuration API)

    本次我们讨论主要聚焦在以下Web.config配置文件的设置值的读取. 1.<connectionString />连接字符串的读取. 2.<appSettings />应用程 ...

  7. ASP.NET Web.config

    分析: .NET Web 应用程序的配置信息(如最常用的设置ASP.Net Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中.当你通过VB.NET新 建 一个Web应用程序后,默认 ...

  8. 【Pro ASP.NET MVC 3 Framework】.学习笔记.5.SportsStore一个真实的程序

    我们要建造的程序不是一个浅显的例子.我们要创建一个坚固的,现实的程序,坚持使它成为最佳实践.与Web Form中拖控件不同.一开始投入MVC程序付出利息,它给我们可维护的,可扩展的,有单元测试卓越支持 ...

  9. ASP.NET web.config中的连接字符串

    在ASP.NET的web.config中,可以用两种方式来写连接字符串的配置. <configuration> <appSettings> <add key=" ...

随机推荐

  1. Android 迷之Version管理

    很多时候会搞混Android中的几个Version minSdkVersion:指你所开发的应用程序能够兼容的最低系统.比如,你现在开发一款暴风.APK,你希望它能在Android已经发布的所有版本的 ...

  2. POJ 2718 穷举

    题意:给定一组数字,如0, 1, 2, 4, 6, 7,用这些数字组成两个数,并使这两个数之差最小.求这个最小差.在这个例子上,就是204和176,差为28. 分析:首先可以想到,这两个数必定是用各一 ...

  3. Apache-Tika解析PDF文档

    通常在使用爬虫时,爬取到网上的文章都是各式各样的格式处理起来比较麻烦,这里我们使用Apache-Tika来处理PDF格式的文章,如下: package com.mengyao.tika.app; im ...

  4. oracle的shutdown命令有几种参数

    SHUTDOWN NORMAL:不允许新的连接.等待会话结束.等待事务结束.做一个检查点并关闭数据文件.启动时不需要实例恢复.SHUTDOWN TRANSACTIONAL:不允许新的连接.不等待会话结 ...

  5. mysql将一个库中表的某几个字段插入到另一个库中的表

    insert into dbname1.tablename1(filed1,filed2,filed3) select filed1,filed2,filed3from dbname2.tablena ...

  6. [转] 浅谈 C++ 中的 new/delete 和 new[]/delete[]

    转:http://www.cnblogs.com/hazir/p/new_and_delete.html 在 C++ 中,你也许经常使用 new 和 delete 来动态申请和释放内存,但你可曾想过以 ...

  7. 关于DOS下启动MySQL时提示服务名无效

    主要原因:启动时:net start mysql 而打开服务后发现,本地服务中mysql这个服务实际名字为mysql55,故启动语句应为:net  start mysql55: 以下摘自课程提问: 你 ...

  8. 反编译 APKTool 逆向助手

    最佳实践--Android逆向助手 1.点击"反编译apk,完成后res下的所有资源就都可以正常使用了,相当于apktool的功能------目前已失效,但是直接用rar解压是可以的!2.点 ...

  9. Eclipse利用代理快速安装插件

    在eclipse启动时增加以下参数: eclipse.exe -vmargs -DproxySet=true -DproxyHost=aProxyAddress -DproxyPort=aProxyP ...

  10. Android ScrollView 嵌套 ListView、 ListView 嵌套ScrollView Scroll事件冲突解决办法

    本人菜鸟一名,最近工作了,开始学习Android. 最近在做项目的时候,UX给了个design,大概就是下拉刷新的ListView中嵌套了ScrollView,而且还要在ScrollView中添加动画 ...