Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dim files = From file In Directory.EnumerateFiles(txtFolder.Text, txtType.Text, SearchOption.AllDirectories)
                        From line In System.IO.File.ReadLines(file) Where line.Contains(txtFindWhat.Text) Select New With {file, line}

        For Each f In files
            Dim item As New ListViewItem($"{f.file}") 'First Column File Localtion
            item.SubItems.Add($"{f}") 'Second Column Add The line number where found the text
            item.SubItems.Add($"{f.line}") 'Third Column Text Search
            ListView1.Items.Add(item) 'Add Records
        Next

How to make in Second column to display the line number where it found in the file the text example: Test.txt asdasd Testa Geter Better

So i search for text "Better" and to display it in column 2 that it found it at line 4 in file


What I have tried:

cannot find solution for this or i do not know exactly how to do it
Posted
Updated 1-Dec-19 1:23am
Comments
Richard MacCutchan 1-Dec-19 6:34am    
Just use a counter to count the lines of text as you read them.

1 solution

If i understand you well, you want to return the number of line where searched text has been found...

So:
C#
string[] lines = {"asdasd", "Testa", "Geter", "Better"};
string tofind = "te";

var result = lines.Select((line, NoOfLine) => new {line, NoOfLine})
	.Where(l => l.line.Contains(tofind))
	.Select(l => l.NoOfLine)
	.ToList();
//returns: {2, 3} 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900