2012-05-21 2 views
2

app.config 파일에 ArrayOfString 항목이 있습니다. 각 항목에는 세미콜론으로 구분 된 문자열이 들어 있습니다. 입력 기준에 따라 List<>의 값을 파싱하려면 가능한 경우 람다를 사용할 수 있기를 원합니다. 하지만 그 기준에 기초한 첫 번째 항목을 원합니다. 아니면 더 좋은 방법이 있습니까 app.config 파일을 사용하고 있습니까? 나는 [source],[filetype]을 포함하고 바로 파일 경로를 반환 첫 번째 항목을 찾을 수 원한다면 예를 들어구분 된 값으로 여러 값이있는 C# app.config

..

.

app.config 항목.

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

DEST;FLAC;112;2;\\destpath\music\ 
+0

ConfigurationManager를 사용하여 app.config에서 값을 가져올 수 있습니다. 이 솔루션과 함께 가고 싶다면 -이 구조의 모든 항목, 즉 소스, 무언가, 숫자, 숫자, 경로입니까? 그렇다면 객체를 만들어 값으로 인스턴스화 할 수 있습니다. 그러면 필터링은 매우 간단합니다. 당신이 찾고자하는 텍스트의 예는 무엇입니까? 그것은 'SOURCE; FLAC'인가 다른 것입니까? –

답변

1

오히려 당신의 값이 문자열 분할 작업의 올바른 인덱스에 떨어 필요에 의존하는 것보다, 당신은 당신의 자신의 ConfigurationSection 정의를 작성해야합니다.

How To on MSDNMSDN ConfigurationProperty example을 참조하십시오. 당신이 당신의 사용자 정의 구성이 지정되면

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에 지정된 속성을 사용하여 람다와 Select : 여기

당신이 시작하는 몇 가지 코드입니다.

관련 문제