VS2005中读写配置文件(方法二)
2008-03-03 15:53
489 查看
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() );
}
}
}
}
<?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() );
}
}
}
}
相关文章推荐
- VS2005中读写配置文件(方法一)(转载)
- VS2005中读写配置文件(方法一)(转载)
- Java Web:JSON 作为配置文件,简单读写的方法
- VS2005运行时读写配置文件(.config)
- C# 读写App.config配置文件的方法
- Python3读写INI配置文件简单方法
- VS2005中读写配置文件(一)
- Andorid读写全局配置文件的方法
- VS2005运行时读写配置文件(.config)
- VS2005运行时读写配置文件(.config)
- C#读写config配置文件的方法
- .NET 2.0中,配置文件app.config的读写(VS2005,C#)
- C# 读写App.config配置文件的方法
- python读写ini配置文件方法实例分析
- 读写配置文件的方法
- [导入].NET 2.0中,配置文件app.config的读写(VS2005,C#)
- VS2005运行时读写配置文件(.config)
- [C#] 读写App.config配置文件的方法
- 用VS2005在Release配置下编译出现清单文件的错误的解决方法
- Python读写配置文件的方法