Net2.0下C#读取任意的配置文件(如:My.config)的问题???

ericken 2006-12-19 03:08:51
学习了.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, 实在不知道该如何解决这个问题, 希望得到高人的指点, 谢谢 :)
...全文
1358 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
ericken 2006-12-20
  • 打赏
  • 举报
回复
能贴出基于My.config文件的代码实现吗? 大家动其手来 :) 我也在操刀中...
Qim 2006-12-20
  • 打赏
  • 举报
回复
msdn:
http://msdn2.microsoft.com/zh-cn/library/system.configuration.configuration(VS.80).aspx
Qim 2006-12-20
  • 打赏
  • 举报
回复
分已收到。多谢楼主。
如何写的代码我这里也有,刚才整理了一下。
这个东西,感觉很有意思。很有用。
ericken 2006-12-20
  • 打赏
  • 举报
回复
Qim(莫名-想星星)也帮了很大的忙 我在另外的帖子里赠您满分 :)
ericken 2006-12-20
  • 打赏
  • 举报
回复
哈哈...实在是无语了...

老实说为了实现这个需求费了我不少功夫, 不想到后来犯了如此低级的错误,除了感谢scow的热心帮忙就只省下惭愧了...

这个贴能结了 赠scow满分!!!!

但还是想把这个问题继续讨论下去, 读取部分有了 大家来看看怎么实现"写"

好东西要分享!!!
Qim 2006-12-20
  • 打赏
  • 举报
回复
哦,忘了说了。我的工程名称改了一下为:testConfig,注意修改相关联的地方。
scow 2006-12-20
  • 打赏
  • 举报
回复
已收到,经测试还是无误。
fileMap.ExeConfigFilename = @"D:\TestApp\My.config";
确保‘D:\TestApp\My.config’这个地址无误,其他我也没啥可说了。
test33 2006-12-20
  • 打赏
  • 举报
回复
路过`
学习了`
Qim 2006-12-20
  • 打赏
  • 举报
回复
搞定,如下:
private void ReadMyConfig()
{
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
string strPath = "d:\\App.config";
fileMap.ExeConfigFilename = strPath ;

if (!File.Exists(strPath ))
{
MessageBox.Show("no file");
}

Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(fileMap , ConfigurationUserLevel.None);
if (cfg.Sections["systems"] == null)
{
return;
}

SystemsSection ss = cfg.GetSection("systems") as SystemsSection;
if (ss == null)
{
return;
}
SystemsCollection oneCollection = ss.Systems;
string sCon = "";
foreach (SystemElement oneElement in oneCollection)
{
sCon+=oneElement.ToString();
}
if (sCon.Length > 0)
{
MessageBox.Show(sCon);
}
}
ericken 2006-12-20
  • 打赏
  • 举报
回复
hi~~scow, 工程已发到你的邮箱(amazon_scow@mail.china.com), 请查收, 费心了 :)
Qim 2006-12-20
  • 打赏
  • 举报
回复
正在调试……
ericken 2006-12-20
  • 打赏
  • 举报
回复
没错 确实让各位大虾觉得我有点舍近求远的味道 用System.Xml是可以解决问题

但是我想要搞明白2.0的System.Configuration里的这些个新鲜玩意儿到底怎么个玩转法

我提供的源代码是可以直接调试的 相信有这样的应用需求的用户是很多的 请大家热烈讨论

帮我解决这个难题 谢谢大家
scow 2006-12-20
  • 打赏
  • 举报
回复
http://www.codeproject.com/useritems/SystemConfiguration.asp
scow 2006-12-20
  • 打赏
  • 举报
回复
看不出有什么问题。如果这样写调用也不行?
void Main(string[] args)
{
ReadAppConfig();
Console.WriteLine(Environment.NewLine);
ReadMyConfig();
}
应该是个很小的地方没注意到。实在不行把工程发过来看看。amazon_scow@mail.china.com
greennetboy 2006-12-20
  • 打赏
  • 举报
回复
config文件是xml格式的,你可以用System.Xml来读取,操作
ericken 2006-12-20
  • 打赏
  • 举报
回复
[My.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>
ericken 2006-12-20
  • 打赏
  • 举报
回复
还是先结贴吧~~~赠scow满分!!!!
scow 2006-12-19
  • 打赏
  • 举报
回复
把My.config贴出来看看
scow 2006-12-19
  • 打赏
  • 举报
回复
你的代码没有问题,如果My.config和App.config内容格式都一样的话,得到的内容也一样。
ericken 2006-12-19
  • 打赏
  • 举报
回复
多谢scow的代码! 但是很惭愧,VB.Net是小弟的弱项:P
可否基于我提供的C#原代码和My.config给出相关的解答
再次感谢
加载更多回复(7)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧