Pro100 Pro100 - 2 months ago 7
C# Question

Line number in file

I'm having some problem with getting line number.

Here is what I got :

var lines = File.ReadLines(fileNameData, Encoding.Default);

foreach (string line in lines)
{
if (line.Contains("()"))
{
MessageBox.Show(line );
}
}


Which show me

MessageBox.Show(line );


So it's shows me lanes which contains (), and it works correctly.
Is there any possibitity to get this line number.

MessageBox.Show(line + lineIndex);


Does anyone know how to accomplish that?

Answer

Try this:

    var lines = File.ReadLines(fileNameData, Encoding.Default);  
    int lineIndex=0;

    foreach (string line in lines)
    {
        if (line.Contains("()"))
        {
            MessageBox.Show(line + lineIndex);
        }
        lineIndex++;
    }
Comments