using(StreamReader rd = File.OpenText("Data.txt")) { string line = rd.ReadLine(); while(line != null) { DoSomething(line); // do more stuff with the line text //move on line = rd.ReadLine(); } }
Dim rd As System.IO.StreamReader = System.IO.File.OpenText("Data.txt") Using (rd) Dim line As String = rd.ReadLine() While (line IsNot Nothing) dosomething(line) ' do more stuff with the line text 'move on line = rd.ReadLine() End While End Using
public static class FileUtil { public static void EachLine(string fileName, Action<string> process) { using(StreamReader rd = File.OpenText(fileName)) { string line = rd.ReadLine(); while(line != null) { process(line); line = rd.ReadLine(); } } } }
Public Class FileUtil Public Shared Sub EachLine(ByVal fileName As String, ByVal process As Action(Of String)) Dim rd As System.IO.StreamReader = System.IO.File.OpenText(fileName) Using (rd) Dim Line As String = rd.ReadLine() While (Line IsNot Nothing) process(Line) Line = rd.ReadLine() End While End Using End SubEnd Class
FileUtil.EachLine("Data.txt", AddressOf dosomething)
Private Sub dosomething(ByVal line As String) MessageBox.Show(line) End Sub
Powered by: newtelligence dasBlog 1.9.7174.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Christiaan Baes
E-mail