我想为C#应用程序创建自定义配置。 app.config喜欢:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="location" type ="FolderConfigSection.LocationConfig, FolderConfigSection"/>
</configSections>
<location>
<folders>
<add folder ="C:\Test1"/>
<add folder ="C:\Test2" />
<add folder ="C:\Test3" />
</folders>
</location>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
所以我创建了两个类。
namespace FolderConfigSection
{
public class LocationConfig : ConfigurationSection
{
[ConfigurationProperty("folders")]
public string Folder
{
get { return base["folder"] as string; }
set { base["folder"] = value; }
}
}
}
并且
namespace FolderConfigSection
{
public class FolderCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new FolderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((FolderElement)element).Environment;
}
}
public class FolderElement : ConfigurationElement
{
[ConfigurationProperty("folder", IsRequired = true)]
public string Environment
{
get { return (string)this["folder"]; }
set { this["folder"] = value; }
}
}
}
但是我的Program.cs中有一个例外
public class Program
{
public static void Main(string[] args)
{
LocationConfig _locationConfig = (LocationConfig)ConfigurationManager.GetSection("location");
string firstFolder = _locationConfig.Folder;
}
}
例外是The section name 'location' is reserved for <location> sections.
我还要列出所有文件夹。
答案 0 :(得分:2)
我正在重述@Tim在评论中回答的内容。
location
作为部分名称。它已经为<location>
元素配置保留。只需使用其他名称,例如locationConfig
。Folder
课程中的LocationConfig
媒体资源类型从String
更改为FolderCollection
。为清楚起见,您应该将属性名称Folder
更改为复数形式Folders
。 LocationConfig
中的所有文件夹,您只需要迭代Folders
集合属性。您可能还希望在FolderCollection
类上实现索引器,以便对Folders
属性进行索引访问。全部放在代码中:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="locationConfig" type ="FolderConfigSection.LocationConfig, FolderConfigSection"/>
</configSections>
<locationConfig>
<folders>
<add folder ="C:\Test1"/>
<add folder ="C:\Test2" />
<add folder ="C:\Test3" />
</folders>
</locationConfig>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
namespace FolderConfigSection
{
using System.Configuration;
public class LocationConfig : ConfigurationSection
{
// Change the property type to FolderCollection, and
// change the property name to `Folders` for clarity
[ConfigurationProperty("folders")]
public FolderCollection Folders
{
get { return base["folders"] as FolderCollection; }
// the setter property isn't really needed here
// set { base["folders"] = value; }
}
}
public class FolderCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new FolderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((FolderElement)element).Environment;
}
// indexer
public FolderElement this[int index]
{
get { return BaseGet(index) as FolderElement; }
}
}
public class FolderElement : ConfigurationElement
{
[ConfigurationProperty("folder", IsRequired = true)]
public string Environment
{
get { return (string)this["folder"]; }
set { this["folder"] = value; }
}
}
}
public class Program
{
static void Main(string[] args)
{
var locationConfig = (LocationConfig)ConfigurationManager.GetSection("locationConfig");
foreach (FolderElement folder in locationConfig.Folders)
{
// List all folders
Console.WriteLine(folder.Environment);
}
// The first folder using indexer property
Console.WriteLine(locationConfig.Folders[0].Environment);
}
}