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
查看全文
相关阅读:
bzoj1009
bzoj1576 3694
bzoj3143
bzoj1391
bzoj2729
bzoj2653
bzoj3261
bzoj2326
人件
优秀的产品
原文地址:https://www.cnblogs.com/mlog/p/2456410.html
最新文章
P3178 [HAOI2015]树上操作
P3805 【模板】manacher算法
P4555 [国家集训队]最长双回文串
bzoj3790 神奇项链
bzoj4305 数列的GCD
P2272 [ZJOI2007]最大半连通子图
P1852 [国家集训队]跳跳棋
SP211 PRIMIT
[手游新项目历程]第2天-webSocket资料
[手游新项目历程]第1天-连不上服务器可能是防火墙
热门文章
[手游新项目历程]第3天-fatal error LNK1000: Internal error during IncrBuildImage
诛仙手游 法宝属性道法性价比
诛仙手游-情缘高攻路线是低道法的最佳选择
[手游新项目历程]第4天-PushFramework资料
诛仙手游-各属性道法换算
svn怎么切换用户
svn怎么切换用户
[手游新项目历程]第5天-解包
bzoj1236
poj3233
Copyright © 2011-2022 走看看