我有一个阅读从应用程序配置自定义部分的问题。 我app.config定制部分阅读
的app.config:
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="StartupFolders" type="Test.FolderConfiguration, Test"/>
</sectionGroup>
</configSections>
<applicationSettings>
<StartupFolders>
<Folders>
<Folder folderType="A" path="c:\foo" />
<Folder folderType="B" path="C:\foo1" />
</Folders>
</StartupFolders>
</applicationSettings>
</configuration>
FolderConfiguration.cs:
namespace Test
{
public class FolderElement : ConfigurationElement
{
[ConfigurationProperty("folderType", DefaultValue = "", IsKey = true, IsRequired = true)]
public string FolderType
{
get { return ((string)(base["folderType"])); }
set { base["folderType"] = value; }
}
[ConfigurationProperty("path", DefaultValue = "", IsKey = false, IsRequired = false)]
public string Path
{
get { return ((string)(base["path"])); }
set { base["path"] = value; }
}
}
[ConfigurationCollection(typeof(FolderElement))]
public class FoldersCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new FolderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((FolderElement)(element)).FolderType;
}
public FolderElement this[int idx]
{
get { return (FolderElement)BaseGet(idx); }
}
}
class FolderConfiguration : ConfigurationSection
{
[ConfigurationProperty("Folders")]
public FoldersCollection FolderItems
{
get { return ((FoldersCollection)(base["Folders"])); }
}
}
}
在MainWindow.cs
最后,我用它都像:
Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
FolderConfiguration section = (FolderConfiguration)cfg.Sections["StartupFolders"];
返回我部分== null。我可以;不明白我做错了什么。任何帮助,请
不,它不。再次获得空格 – amplifier
没有例外或非凡的输出。程序正常工作。只需folderConfig == null。乍一看,我和上面的页面几乎一样 – amplifier