- 浏览: 531439 次
- 性别:
- 来自: 天津
文章分类
- 全部博客 (230)
- java (87)
- c/c++/c# (39)
- ASP.net MVC (4)
- eclipse/visual studio (3)
- tomcat/weblogic/jetty (13)
- linux/unix/windows (20)
- html/javascript/jquery/kendo/bootstrap/layui/vue/react (31)
- hibernate/struts/spring/mybatis/springboot (21)
- lucene/solr/ELK (2)
- shiro (0)
- oracle/sqlserver/mysql/postgresql (23)
- shell/python/ruby (6)
- android (0)
- maven/ant (1)
- freemarker/thymeleaf/velocity (1)
- open source project (41)
- cache/memcached/redis (0)
- nosql/hadoop/hbase/mongodb (0)
- system architecture/dubbo/zookeeper (0)
- software testing (0)
- system optimization (0)
- system security (0)
- tcp/udp/http (2)
- roller/wordpress (2)
- 工具收藏 (8)
- 文摘 (4)
- 生活 (0)
最新评论
-
coconut_zhang:
这个demo 非常完整了,是指下面说的那个html 模版,模版 ...
flying sauser, thymeleaf实现PDF文件下载 -
a93456:
你好,你有完整的demo吗? String template这 ...
flying sauser, thymeleaf实现PDF文件下载 -
yujiaao:
fn 函数循环是没有必要的啊,可以改成
protecte ...
Java 笛卡尔积算法的简单实现 -
安静听歌:
设置了.setUseTemporaryFileDuringWr ...
使用jxl导出大数据量EXCEL时内存溢出的解决办法 -
q280499693:
写的很详细,但是我现在想知道他们是怎么定位log4j.prop ...
关于SLF4J结合Log4j使用时日志输出与指定的log4j.properties不同
在1.0/1.1版本,都流行的是appSettings这个节点读取;无论web.config,还是app.exe.config都可以使用这个节点配置。
如:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="TestKey" value="(local)"/>
<add key="WoniuguDBName" value="woniugu_test"/>
</appSettings>
<!--还有其他的-->
</configuration>
在代码中读取:
string 内容 = ConfigurationSettings.AppSettings[key];
这种方式,简洁;
还有一种:自定义配置节
举例说明:
<configuration>
<configSections>
<section name="sampleSection"
type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<sampleSection setting1="Value1" setting2="value two"
setting3="third value" />
</configuration>
代码中读取:ConfigurationSettings.GetConfig("sampleSection");其返回的对象强制转换为 IDictionary 对象
IDictionary sampleTable = (IDictionary) ConfigurationSettings.GetConfig("sampleSection");
string value1 = (string)sampleTable["setting1"];
string value2 = (string)sampleTable["setting2"];
string value3 = (string)sampleTable["setting3"];
+++++++++++++++
在2.0版本,web.config还是流行appSettings来配置,但app.exe.config采用了Section节点配置。读取类也发生了改变。
在代码中读取:
string 内容 = ConfigurationManager.AppSettings[key];
后面,我简单测试了下,在app.exe.config中还是可以手工添加appSettings来配置。同样用上面的代码来访问,没有问题。
这里,我阅读了一篇文章.Net 自定义应用程序配置,http://www.tracefact.net/CLR-and-Framework/Custom-Application-Configuration.aspx
不错,上面很好讲解。
在此,我只贴我自己分析后写出的部分代码,用来显示如何提取,当然和《.Net 自定义应用程序配置》的内容不一样:
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SetConfig.Properties.Settings" type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser"
requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SetConfig.Properties.Settings" type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<SetConfig.Properties.Settings>
<setting name="userSetting1" serializeAs="String">
<value>user</value>
</setting>
</SetConfig.Properties.Settings>
</userSettings>
<applicationSettings>
<SetConfig.Properties.Settings>
<setting name="appSetting1" serializeAs="String">
<value>application</value>
</setting>
</SetConfig.Properties.Settings>
</applicationSettings>
<appSettings>
<add key="TestKey" value="(local)"/>
<add key="WoniuguDBName" value="woniugu_test"/>
</appSettings>
</configuration>
部分读取代码:
第一种,直接读取,注意上面的配置的类型System.Configuration.ClientSettingsSection
ClientSettingsSection config = (ClientSettingsSection)
ConfigurationManager.GetSection("userSettings/SetConfig.Properties.Settings");
if (config == null)
return ;
SettingElementCollection d = config.Settings;
if (d != null)
foreach(SettingElement de in d)
{
Console.WriteLine("n={0}" ,de.Name);
Console.WriteLine("serializeAs={0}" ,de.SerializeAs);
Console.WriteLine("v={0}" ,de.Value.ValueXml.InnerText);
}
第二种,读取xml内容,需要再次读取。这种方式个人不建议。
ConfigurationSection sectionV;
// Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None) as Configuration;
sectionV =
config.GetSection("userSettings/SetConfig.Properties.Settings") as
ConfigurationSection ;
Console.WriteLine("Section name: {0},raw:{1}",
sectionV.SectionInformation.Name ,sectionV.SectionInformation.GetRawXml() );
关于自己写的这两类读取,本人赞同第一种,方便,直接获取节点值;第二种就在于再次分析xml,也许直接呈现是有用的。
而《.Net 自定义应用程序配置》所写的,也是好东西。建议使用。
总结:
关于自己写的这两类读取,本人赞同第一种,方便,直接获取节点值;
建议的另一点:还是appSettings读取。
最后说明一点:
无论哪类读取,目的都是为了获取配置;从根本上来说:就是xml的操作。这一点才是根本的。
喜欢用appSettings还是用它吧,简单方便。
至于2.0推荐的方式(msdn)也是好用,让用户自己继承接口,自己写读取,实用,只是代码多了点哦,呵呵
发表评论
-
CMPP短信网关客户端发送程序
2010-04-06 11:12 4714最近无聊,玩了玩ubuntu ... -
C#编程中的预处理指令
2010-01-13 11:22 1400在C#中, 虽然编译器没有单独的预处理器,但在处理预处理指令时 ... -
vs 打包winform程序步骤经验总结
2010-01-06 09:47 58441、 应用程序文件夹 : 在应用程序文件夹下新建一文件夹重名为 ... -
处理资源 'http://localhost/ws/Service.asmx' 时出错(iis,.netframework安装顺序造成的错)
2009-12-30 17:10 1908刚刚学习webservices,很多东西都不懂。写了个最简单的 ... -
图解在VS2005利用NUnit进行测试
2009-12-21 12:53 1555一、介绍NUnit是一款用于在.net环境进行单元测试的工具, ... -
如何编写单元测试用例(转载自CSDN)
2009-12-17 17:01 3161一、 单元测试的概念 ... -
NUnit2.0详细使用方法
2009-12-17 17:00 10675 .测试生命周期合约 如果记得test case的定义,其中 ... -
NUnit2.0详细使用方法
2009-12-17 16:58 11621、初识NUnitNUnit是一个开源单元测试框架,它提高一套 ... -
在.NET环境中使用单元测试工具Nunit
2009-12-17 16:56 1056简介编写单元测试是一 ... -
.NET命名空间使用介绍
2009-12-17 11:29 1710在ASP.NET中,.NET Framework为我们 ... -
一种清晰而又简单通用的分页算法
2009-12-17 10:03 6089分页,是WEB开发中面对的最常见的编程,实现方法多种多样。我也 ... -
C#数据结构-双向链表
2009-12-17 09:54 1956在结点中设两个引用域,一个保存直接前驱结点的地址,叫prev, ... -
读书笔记c#高级编程 委托和事件
2009-12-17 09:49 1096在C#中使用一个类时,分两个阶段。首先需要定义这个类,即告诉编 ... -
深入理解C#中的委托和事件:委托的定义
2009-12-16 16:04 1641委托和事件在 .Net Framework中的应用非常广泛,然 ... -
C#委托和事件(简单示例)
2009-12-16 15:46 38291.委派的实现过程。 首先来看一下委派,委派其实就是方法的传 ... -
C# WInForm 无框窗体移动
2009-11-12 13:56 2014调用APIusing System.Runtime.Inter ... -
在C# 中如何动态地添加控件
2009-09-09 11:05 3647通常在写程序的时候 ... -
ListBox相互移动
2009-08-14 17:33 1429using System;using System.Colle ... -
TreeView-demo
2009-08-14 17:22 1317using System;using System.Co ... -
TreeView高效绑定数据
2009-08-13 17:28 4099前俩个项目都是做政府的门户网,他们的栏目、部门都要以树形展示, ...
相关推荐
保存INI配置文件和读取配置文件,欢迎大家下载学习!
qt 界面,简单的文件读取 写入,欢迎交流学习
通过按钮,读取配置文件,并修改自定义配置文件appSettings中的值.这是一个很简单的dome.
读取XXX.ini配置文件里面数据信息 1.在使用中把CIniFile类添加到项目当中,并在使用的类引用CIniFile类 2.m_iniFile = new CIniFile(QCoreApplication::applicationDirPath()+ MoonlightService); [MoonlightService...
java读取配置文件 在经常更换的一些东西 就可以用配置文件
c# 实现的。通过配置文件config 读取数据库连接字符串,用户名密码等。通过配置文件,有利于系统的升级。
visual C++2010下写的纯C++代码写的读写配置文件源代码
我们使用Spring Boot编写多个模块开发时,我们希望各个模块使用各自的配置文件,避免将整个项目的配置文件都写在一起,从而降低各个模块之间的耦合度。
C# .net读取ini配置文件
系统开发的时候,都会涉及到一些配置文件的读取,为方便从配置文件中读取我们预先设置的值,我做了一个简单配置文件读取工具类。这个类主要基于apache开源项目commons-configuration,附件是该工具类完整代码,仅供...
实现配置文件读取的C++代码,可直接在程序中使用,免去自己重新编写的麻烦,快捷方便。
在开发Asp.net站点的时候,我们会遇到很多的配置参数:网站名称,上传图片后缀,上传文件后缀,关键字过滤,数据库连接字串等等,这些内容如果比较少的话,直接配置到Web.config文件中,借由.NET提供的操作类,将会非常方便的...
Qt读取配置文件的源码
读取文件,C++ 源码 可读取INI配置文件
自己写的vbs读取配置文件的代码 直接就能用 很好使哈
c++读取XML配置文件C++中并没有操作XML文件的标准库,因此大家需要使用各自熟悉的XML库来解决XML文件的读取与写入。XML的一个重要用途是作为程序的配置文件,存储程序运行相关的各种数据。本文总结了使用libxml2库来...
自做的读取xml配置文件的flash导航菜单,带fla源文件。
本代码示例如何使用Java来读取configuration配置文件中的内容
spring读取配置不同目录下的配置文件,使用ClassPathXmlApplicationContext与FileSystemXmlApplicationContext