static long CountLines(string fileName)
{
char[] buffer = new char[32 * 1024]; //lit 32K char à chaque fois
System.IO.TextReader reader;
int total = 1; //tout fichier contient au moins une ligne
int read;
if (System.IO.File.Exists(fileName))
{
reader = System.IO.File.OpenText(fileName);
while ((read=reader.Read(buffer, 0, buffer.Length)) > 0)
{
for (int i = 0; i < read; i++)
{
if (buffer[i] == '\n')
{
total++;
}
}
}
//nettoyage des variables
reader.Close();
reader.Dispose();
reader = null;
}
return total;
}
//exemple d'utilisation :
MessageBox.Show(CountLines(@"C:\AVANCE.LOG").ToString());