Sunday, January 25, 2009

count lines in your text file

This next block of code counts the lines in a file on the disk. It does this by using the ReadLine() method in the .NET framework 3.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;


namespace avaricesoft
{
class professionalcoder
{
// must include using System.IO; before namespace because streamreader and writer are inherit from them
// count lines in your text file give filename in parameter
public long CountLinesInFile(string f)
{
long count = 0;
using (StreamReader r = new StreamReader(f))
{
string line;
while ((line = r.ReadLine()) != null)
{
count++;
}
}
return count;
}

// count lines in your text file give stream read object in your file
public long countlines(StreamReader sr)
{
string line;
while ((line = sr.ReadLine()) != null)
{
count++;
}
}
return count;
}

}
}

No comments:

Post a Comment