Thomas Darimont
Erfahrenes Mitglied
Hallo!
Schaut mal hier:
Gruß Tom
Schaut mal hier:
C#:
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