2

我的app.config文件有一个ArrayOfString条目。每个条目都包含一个分号分隔的字符串。如果可能的话,我希望能够使用 lambdaList<>根据输入条件解析出 a 中的值。但我希望它找到的第一个条目基于该标准。或者有没有更好的方法使用app.config文件?

例如 ..

如果我想找到包含的第一个条目[source],[filetype]并返回文件路径。

示例app.config条目。

SOURCE;FLAC;112;2;\\sourcepath\music\

DEST;FLAC;112;2;\\destpath\music\
4

1 回答 1

1

您应该创建自己的ConfigurationSection定义,而不是依赖于让您的值落在字符串拆分操作的正确索引处。

请参阅MSDN 上的 How ToMSDN 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; }
    }
}

指定自定义配置后,您可以Select使用自定义中指定的任何属性使用 lambda ConfigurationElement

于 2012-05-21T20:27:59.003 回答