请教"关于.Net2.0下配置架构的使用"的问题
Fandango
您好,拜读您的博客园里名为"关于.Net2.0下配置架构的使用"的文章,十分受益。这两天我在写一个应用程序配置模块的时候碰到了一点问题,想跟您请教:)
您在"关于.Net2.0下配置架构的使用"的文中介绍了ConfigurationSection, ConfigurationElementCollection和ConfigurationElement这几个类, 我把它们应用到了我的读取App.config配置文件的代码中, 效果不错. 我的 App.config文件的内容和源程序如下:
[App.config]:
------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="systems" type="TestApp.Configuration.SystemsSection, TestApp" />
</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>
[源程序]:
------------------------------------------------------
[SystemsSection.cs]:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
namespace TestApp.Configuration
{
public sealed class SystemsSection : ConfigurationSection
{
public SystemsSection()
{
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public SystemsCollection Systems
{
get
{
return (SystemsCollection)base[""];
}
}
}
}
[SystemsCollection.cs]:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
namespace TestApp.Configuration
{
public sealed class SystemsCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new SystemElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((SystemElement)element).Name;
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName
{
get
{
return "system";
}
}
}
}
[SystemElement.cs]:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
namespace TestApp.Configuration
{
public sealed class SystemElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get
{
return (string)base["name"];
}
set
{
base["name"] = value;
}
}
[ConfigurationProperty("server", IsRequired = true)]
public string Server
{
get
{
return (string)base["server"];
}
set
{
base["server"] = value;
}
}
[ConfigurationProperty("database", IsRequired = true)]
public string Database
{
get
{
return (string)base["database"];
}
set
{
base["database"] = value;
}
}
public override string ToString()
{
string output = "SystemElement :\n";
output += string.Format("Name = {0}\n", Name);
output += string.Format("Server = {0}\n", Server);
output += string.Format("Database = {0}\n", Database);
return output;
}
}
}
------------------------------------------------------
[Demo]:
public void ReadAppConfig()
{
ConfigurationSection sysSection = ConfigurationManager.GetSection("systems") as ConfigurationSection;
SystemsSection ss = (SystemsSection)sysSection;
SystemsCollection oneCollection = ss.Systems;
foreach (SystemElement oneElement in oneCollection)
{
Console.WriteLine(oneElement.ToString());
}
}
------------------------------------------------------
[OUTPUT]:
SystemElement :
Name = Production
Server = PRODSERVER
Database = Prod
SystemElement :
Name = Demo
Server = DEMOSERVER
Database = Demo
SystemElement :
Name = Testing
Server = TESTSERVER
Database = Test
SystemElement :
Name = Development
Server = DEVSERVER
Database = DEV
------------------------------------------------------
程序写到这里一直都挺顺利,但是我有另外的需求需要实现,我想要把上述App.config配置文件中的内容保存在自定义的配置文件中用于程序运行时读取,比如D:\TestApp\My.config文件.根据你文章中的提供的例程我另外写了一段Demo代码:
------------------------------------------------------
[Demo]:
public void ReadMyConfig()
{
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"D:\TestApp\My.config";
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
SystemsSection ss = cfg.GetSection("systems") as SystemsSection
SystemsCollection oneCollection = ss.Systems;
foreach (SystemElement oneElement in oneCollection)
{
Console.WriteLine(oneElement.ToString());
}
}
但是“cfg.GetSection("systems") as SystemsSection”语句的返回值为null, 实在不知道该如何解决这个问题, 希望得到您的指点, 谢谢 :)
真是不好意思,这一段时间忙,没有看见这个留言,现在回答,也不知道你能看见不。
你说通过GetSection("")返回的Section为Null,我觉得你应当在这里设置一个断点,查看一下,你读取到的Cfg配置里倒底有哪些Session存在。我想这样可以很容易的找出其中的问题来。
两个易发生的问题,Sessions中不存在Systems这个Session,检查一下文件,或是配置类型不对。应该很容易查出问题。
我在程序中是用
cfg.Sessions["systems"] as SystemsSection进行读取的。
希望能帮到你。