Hallo!

Schaut mal hier:
Code csharp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace De.Tutorials.Training
{
    public class TextFileReaderExample
    {
        public static void Main(string[] args)
        {
            foreach(string line in new TextFile(@"c:\zitate.txt")){
                Console.WriteLine(line);
            }
        }
    }
 
    class TextFile : IEnumerable<string>
    {
        string pathToFile;
 
        public TextFile(string pathToFile)
        {
            this.pathToFile = pathToFile;
        }
 
        #region IEnumerable<string> Members
 
        public IEnumerator<string> GetEnumerator()
        {
            StreamReader streamReader = File.OpenText(this.pathToFile);
            using (streamReader)
            {
                string line;
                while (null != (line = streamReader.ReadLine()))
                {
                    yield return line;
                }
            }
        }
 
        #endregion
 
        #region IEnumerable Members
 
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
 
        #endregion
    }
}

Gruß Tom