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
查看全文
相关阅读:
C#(99):TreadPool
C#(99):多线程锁:Mutex互斥体,Semaphore信号量,Monitor监视器,lock,原子操作InterLocked
C#(99):Thead线程 System.Thread
ASP.NET(99):WebService之WebMethod参数介绍
C#(99):WCF之WebService
Windows服务 System.ServiceProcess.ServiceBase类
应用程序域 System.AppDomain,动态加载程序集
C#(99):app.config/web.config配置文件增删改
C#(99):文件读写(三)利用FileStream类操作字节数组byte[]、BinaryFormatter、内存流MemoryStream
C#(99):文件读写(二)利用SteamReader和StreamWrite类处理字符串、FileSystemWatcher、BinaryReader/BinaryWriter
原文地址:https://www.cnblogs.com/mlog/p/2456410.html
最新文章
ASP.NET(99):Cookie操作、ASP.Net文件上传HttpPostedFile
ASP.NET(99):ASP.Net模拟用户 System.Security.Principal
ASP.NET(99):Web缓存Cache、Application、Session与ViewState
ASP.NET(99):ASP.Net页面生存周期、Page_Load方法
ASP.NET(99):HTTP请求处理流程、IHttphandler、IHttpModule
C#(99):WCF错误处理
ASP.NET(99):ASP.Net用户控件的使用
C#(99):WCF双通信
C#(99):DataSet,DataTable,DataView、DataRelation
C#(99):WCF Windows基础通信
热门文章
C#(99):SQLCommand命令、DbTransaction事务
C#(99):DataAdapter
C#(99):ADO.Net数据库连接字符串、DbProviderFactory
设置窗口在最前
C#(99):属性 Attribute
C#(99):反射基础 System.Reflection
C#(99):LINQ to DataSet,对离线数据的Linq支持、AsEnumeable()
C#(99):微软Charting图表控件 System.Web.UI.DataVisuliztion.Charting
C#(99):异步和多线程的区别
BackGroundWorker组件使用、Winform控件的Invoke安全调用
Copyright © 2011-2022 走看看