문제

TextReader source에서 어떻게 반복합니까?

나는 시도 를 시도했다

foreach (var line in source)
.

그러나 오류가 발생했습니다

foreach 문은 'system.io.textreader'에 'getEnumerator'에 대한 공개 정의가 없기 때문에 'system.io.textreader'유형의 변수에서 작동 할 수 없습니다.

도움이 되었습니까?

해결책

string line;
while ((line = myTextReader.ReadLine()) != null)
{
    DoSomethingWith(line);
}
.

다른 팁

지연 실행 메소드 인 File.ReadLines를 사용한 다음 줄을 루프 :

foreach (var line in File.ReadLines("test.txt"))
{
}
.

자세한 정보 :

http://msdn.microsoft.com/en-us/library/DD383503.aspx

ReadLine method 를 기반 으로이 코드를 사용해 볼 수 있습니다.

        string line = null;
        System.IO.TextReader readFile = new StreamReader("...."); //Adjust your path
        while (true)
        {
            line = readFile.ReadLine();
            if (line == null)
            {
                break;    
            }
            MessageBox.Show (line);
        }
        readFile.Close();
        readFile = null;
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top