zoukankan
html css js c++ java
VS2005中读写配置文件(方法二)
File = App.config
<?
xml version="1.0" encoding="utf-8"
?>
<
configuration
>
<
configSections
>
<
section
name
="systems"
type
="ConfigConsoleApplication.SystemsSection, ConfigConsoleApplication"
/>
</
configSections
>
<
systems
>
<
system
name
="Production"
server
="PRODSERVER"
database
="Prod"
/>
<
system
name
="Demo"
server
="DEMOSERVER"
database
="Demo"
/>
<
system
name
="Testing"
server
="TESTSERVER"
database
="Test"
/>
<
system
name
="Development"
server
="DEVSERVER"
database
="DEV"
/>
</
systems
>
</
configuration
>
File = Program.cs
using
System;
using
System.Collections.Generic;
using
System.Collections.Specialized;
using
System.Configuration;
//
remember to add reference in project
using
System.Reflection;
using
System.Text;
namespace
ConfigConsoleApplication
...
{
public
sealed
class
SystemsSection : ConfigurationSection
...
{
public
SystemsSection()
...
{
}
[ConfigurationProperty(
""
, IsDefaultCollectionProperty
=
true
)]
public
SystemsCollection Systems
...
{
get
...
{
return
(SystemsCollection)
base
[
""
];
}
}
}
public
sealed
class
SystemsCollection : ConfigurationElementCollection
...
{
protected
override
ConfigurationElement CreateNewElement()
...
{
return
new
SystemElement();
}
protected
override
object
GetElementKey( ConfigurationElement element )
...
{
return
( (SystemElement)element ).Name;
}
protected
override
ConfigurationElementCollectionType CollectionType
...
{
get
...
{
return
ConfigurationElementCollectionType.BasicMap;
}
}
protected
override
string
ElementName
...
{
get
...
{
return
"
system
"
;
}
}
}
public
sealed
class
SystemElement : ConfigurationElement
...
{
[ConfigurationProperty(
"
name
"
, IsCollectionKey
=
true
, RequiredValue
=
true
)]
public
string
Name
...
{
get
...
{
return
(
string
)
base
[
"
name
"
];
}
set
...
{
base
[
"
name
"
]
=
value;
}
}
[ConfigurationProperty(
"
server
"
, RequiredValue
=
true
)]
public
string
Server
...
{
get
...
{
return
(
string
)
base
[
"
server
"
];
}
set
...
{
base
[
"
server
"
]
=
value;
}
}
[ConfigurationProperty(
"
database
"
, RequiredValue
=
true
)]
public
string
Database
...
{
get
...
{
return
(
string
)
base
[
"
database
"
];
}
set
...
{
base
[
"
database
"
]
=
value;
}
}
public
override
string
ToString()
...
{
string
output
=
"
SystemElement :
"
;
output
+=
string
.Format(
"
Name = {0}
"
, Name );
output
+=
string
.Format(
"
Server = {0}
"
, Server );
output
+=
string
.Format(
"
Database = {0}
"
, Database );
return
output;
}
}
public
class
Program
...
{
static
void
Main(
string
[] args )
...
{
SystemsSection sysSection
=
ConfigurationManager.GetSection(
"
systems
"
)
as
SystemsSection;
SystemsCollection oneCollection
=
sysSection.Systems;
foreach
( SystemElement oneElement
in
oneCollection )
...
{
Console.WriteLine( oneElement.ToString() );
}
}
}
}
靓点博客 http://www.cnblogs.com/mlog 或 http://blog.csdn.net/cml2030
查看全文
相关阅读:
SRM 588 D2 L3:GameInDarknessDiv2,DFS
[置顶] ProDinner体验
[置顶] 强制访问控制内核模块Smack
Java小项目--坦克大战(version1.0)
utf-8-validation
is-subsequence
perfect-rectangle
find-the-difference
longest-absolute-file-path
first-unique-character-in-a-string
原文地址:https://www.cnblogs.com/mlog/p/2456410.html
最新文章
Flash的选择
listView中adapter有不同的click事件的简单写法
鸟书shell 学习笔记(二) shell中正則表達式相关
uva 10817
学术达人
【读书笔记】——《A Brief History of Humankind》
【读书笔记】——《A Brief History of Humankind》
机器学习: Python with Recurrent Neural Network
地道的英语 —— 否定、副词、褒贬义、情态动词与语气
地道的英语 —— 否定、副词、褒贬义、情态动词与语气
热门文章
OpenGL(二十三) 各向异性纹理过滤
【数学】十万个为什么(一) —— 为什么乘法会分为左乘和右乘,除法会分为左除和右除?
【数学】十万个为什么(一) —— 为什么乘法会分为左乘和右乘,除法会分为左除和右除?
matlab 工具函数 —— normalize(归一化数据)
使用gfortran将数据写成Grads格式的代码示例
SRM 588 D2 L2:GUMIAndSongsDiv2,冷静思考,好的算法简洁明了
[置顶] Android安全机制分析
Eclipse+Java+OpenCV246人脸识别
再看ADO对象模型
Windows下文件列举,搜索
Copyright © 2011-2022 走看看