【发布时间】:2010-08-11 22:40:09
【问题描述】:
以下是我的课程,感谢以下文章:
我的代码:
public class FavsSection : ConfigurationSection
{
public override bool IsReadOnly()
{
return base.IsReadOnly();
}
public FavsSection() // default Constructor.
{ }
[ConfigurationProperty("Items", IsRequired=true)]
public FavouritesCollection FavsItems
{
get
{
return (FavouritesCollection)(base ["Items"]);
}
}
}
[ConfigurationCollection(typeof(FavouriteElement))]
public class FavouritesCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new FavouriteElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((FavouriteElement)(element)).ItemType;
}
public FavouriteElement this[int idx]
{
get
{
return (FavouriteElement)BaseGet(idx);
}
}
public override bool IsReadOnly()
{
return base.IsReadOnly();
}
}
public class FavouriteElement : ConfigurationElement
{
[ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
public string ID
{
get
{
return ((string)(base["id"]));
}
set
{
base["id"] = value;
}
}
[ConfigurationProperty("path", DefaultValue = "", IsKey = false, IsRequired = false)]
public string Path
{
get
{
return ((string)(base["path"]));
}
set
{
base["path"] = value;
}
}
}
我的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="FavouritesMenu" type="MyFileExplorer.FavsSection, MyFileExplorer" />
</configSections>
<FavouritesMenu>
<Items>
<add id="1" path="c:\foo" />
<add id="2" path="C:\foo1" />
</Items>
</FavouritesMenu>
</configuration>
如您所见,我正在尝试将数据写入名为“收藏夹菜单”的自定义部分。我想我已经掌握了这个想法的基本要点,但我不知道如何进行下一步......与“IsReadOnly”方法有关吗?有人可以帮我填空吗?随意重命名内容以使其更易于阅读?在寻求帮助之前,我以为我会做出一半体面的努力......
研究:StackOverFlow - SAME QUESTION!
---------- Pike65 的评论迷路了...无法写入集合,因为它被设置为只读。
我认为集合需要设置为 IsReadOnly false 并且需要一些辅助方法来将数据添加到集合中?这部分对我来说有点朦胧......
感谢阅读, 伊布拉尔
【问题讨论】:
标签: c# .net-3.5 configuration app-config