[英]C# app.config with multiple values with a delimited value
我的app.config
文件帶有ArrayOfString
條目。 每個條目均包含一個以分號分隔的字符串。 我希望能夠使用lambda(如果可能)根據輸入標准從List<>
解析出值。 但是我希望它根據該標准找到第一個條目。 還是有使用 app.config
文件的更好方法?
例如 ..
如果我想查找包含[source],[filetype]
的第一個條目,然后返回文件路徑。
示例app.config
條目。
SOURCE;FLAC;112;2;\\sourcepath\music\
DEST;FLAC;112;2;\\destpath\music\
您應該創建自己的ConfigurationSection
定義,而不是依賴於值落在字符串拆分操作的正確索引上。
請參閱“ 如何在MSDN上”和“ MSDN ConfigurationProperty”示例 。
以下是一些入門代碼:
class CustomConfig : ConfigurationSection
{
private readonly CustomElementCollection entries =
new CustomElementCollection();
[ConfigurationProperty("customEntries", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(CustomElementCollection), AddItemName = "add")]
public CustomElementCollection CustomEntries { get { return entries; } }
}
class CustomElementCollection : ConfigurationElementCollection
{
public CustomElement this[int index]
{
get { return (CustomElement) BaseGet(index); }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new CustomElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((CustomElement)element).Name;
}
}
class CustomElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get { return this["name"] as string; }
set { this["name"] = value; }
}
[ConfigurationProperty("direction", IsRequired = true)]
public string Direction
{
get { return this["direction"] as string; }
set { this["direction"] = value; }
}
[ConfigurationProperty("filePath", IsRequired = true)]
public string FilePath
{
get { return this["filePath"] as string; }
set { this["filePath"] = value; }
}
}
一旦指定了自定義配置,就可以使用在自定義ConfigurationElement
指定的任何屬性使用lambda進行Select
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.