I want to get a specified line from text using ReadLine method
Here is my code
for (int p = 0; p < 100; p++)
{
var files = await ApplicationData.Current.LocalFolder.GetFileAsync(logid.Text + ".txt");
var lines = await FileIO.ReadLinesAsync(files);
var pattern = sm.SelectedItem.ToString();
var sline = lines[p].Contains(pattern);
lv.items.add(lines[sline]);
}
The problem is var sline is not converted into int so lines cannot read it.
The actual program is to add text only from line contained pattern from a selected combobox.
Your problem is you need to search the list for the line matching pattern. Your code just looks through the first 100 lines and sees if any of them contains the text you want. the Contains() method returns a bool – not a string.
I’m not entirely sure why you’re looping to 100 arbitrarily? Is the file you’re reading always going to have exactly 100 lines? I think your are looking for more something like this? You can drop the loop completely.
if you may have more than 1 line matching the pattern, you will want to iterate over all of those results using linq’s Where().