How to read only one line of a file in C #

1

I know how to read the whole file but how can I read only one line

EX: read line 3 only

    
asked by anonymous 02.04.2018 / 03:32

2 answers

1

By the% of the line, eg:

string[] allLines = File.ReadAllLines(filePath);

string linha1 = allLines[0];
string linha2 = allLines[1];
string linha3 = allLines[2];
string linha4 = allLines[3];
    
02.04.2018 / 03:36
1

You will have to first select all rows and then through the index choose which one you want

var file = new StreamReader(caminhoArquivo);
string[] linhas = File.ReadAllLines(caminhoArquivo);
var textoDesejado = linhas[2];
file.Close();

Note that to select line 3, use index 2 and finally, do not forget to close the file.

    
02.04.2018 / 03:40