C#自定义ConfigSections节 操作 。
c# 操作 自定义
2023-09-11 14:16:45 时间
sectiongroup:
在config文件中加入以下节点:
- <configSections>
- <sectionGroup name="WebSiteInfo">
- <section name="basicInfo" type="ConfigurationSectionTest.WebSiteInfoHandler"/>
- <section name="fileUpload" type="ConfigurationSectionTest.WebSiteInfoHandler"/>
- </sectionGroup>
- </configSections>
- <WebSiteInfo>
- <basicInfo>
- <add key="name" value="huchen's homepage"/>
- <add key="version" value="1.0"/>
- </basicInfo>
- <fileUpload>
- <add key="fileUploadPath" value="E:\\MyHomePage\\Web\\Upload\\"/>
- <add key="fileUploadSizeMax" value="2M"/>
- </fileUpload>
- </WebSiteInfo>
<configSections>
<sectionGroup name="WebSiteInfo">
<section name="basicInfo" type="ConfigurationSectionTest.WebSiteInfoHandler"/>
<section name="fileUpload" type="ConfigurationSectionTest.WebSiteInfoHandler"/>
</sectionGroup>
</configSections>
<WebSiteInfo>
<basicInfo>
<add key="name" value="huchen's homepage"/>
<add key="version" value="1.0"/>
</basicInfo>
<fileUpload>
<add key="fileUploadPath" value="E:\\MyHomePage\\Web\\Upload\\"/>
<add key="fileUploadSizeMax" value="2M"/>
</fileUpload>
</WebSiteInfo>
获取其中的值
- string sectionGroup = "WebSiteInfo";
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- foreach (ConfigurationSection cfgSection in config.GetSectionGroup(sectionGroup).Sections)
- {
- string strSectionName = cfgSection.SectionInformation.Name;
- Hashtable ht = ConfigurationManager.GetSection(sectionGroup + "/" + strSectionName) as Hashtable;
- if (strSectionName=="basicInfo")
- {
- string strSite = ht["name"].ToString();
- string strWeb = ht["version"].ToString();
- }
- }
string sectionGroup = "WebSiteInfo";
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (ConfigurationSection cfgSection in config.GetSectionGroup(sectionGroup).Sections)
{
string strSectionName = cfgSection.SectionInformation.Name;
Hashtable ht = ConfigurationManager.GetSection(sectionGroup + "/" + strSectionName) as Hashtable;
if (strSectionName=="basicInfo")
{
string strSite = ht["name"].ToString();
string strWeb = ht["version"].ToString();
}
}
section:
在config文件中加入以下节点:
- <configuration>
- <configSections>
- <section name="sampleSection"
- type="System.Configuration.SingleTagSectionHandler" />
- </configSections>
- <sampleSection setting1="Value1"
- setting2="value two"
- setting3="third value" />
- </configuration>
<configuration>
<configSections>
<section name="sampleSection"
type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<sampleSection setting1="Value1"
setting2="value two"
setting3="third value" />
</configuration>
获取其中的值
- Hashtable ht = ConfigurationManager.GetSection("sampleSection") as Hashtable;
- string str1=ht["setting1"];
Hashtable ht = ConfigurationManager.GetSection("sampleSection") as Hashtable;
string str1=ht["setting1"];
http://technet.microsoft.com/zh-cn/magazine/ms228245(VS.90).aspx
相关文章
- C# MarshalByRefObject 和Serializable的区别
- C#在后台运行操作:BackgroundWorker的用法
- Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)
- C# PDF Page操作——设置页面切换按钮 C# 添加、读取Word脚注尾注 C#为什么不能像C/C++一样的支持函数只读传参 web 给大家分享一个好玩的东西,也许你那块就用的到
- Word控件Spire.Doc 【文档操作】教程(五):在 C#、VB.NET 中合并、设置多个 Word 文档
- 【C#】return (T1, T2);
- c#重载运算符
- 倍加福二维码测试2——C#界面程序开发
- C#用dynamic一行代码实现反射操作
- C#自定义ConfigSections节点操作
- C#连接OleDBConnection数据库的操作
- C#:DataTable映射成Model
- C#常用字符串操作类处理与归纳
- C# DataSet的基本用法
- c#基础操作
- 《C#零基础入门之百识百例》(七十七) 委托事件实例练习2 -- 刘备招亲甘露寺
- 使用C#创建Windows服务
- C# 字符串操作详解
- C#根据汉字获取编码和根据编码获取汉字
- 归纳一下:C#线程同步的几种方法
- C#在方法或属性中使用sealed时的操作与原理
- C#与.NET Framework c#编程语言,和java是一样的。(c#,java) -->javaweb,asp.net