How to read only one line from a file in c#

Asked

Viewed 173 times

1

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

EX: read line 3 only

2 answers

1


For index line, example:

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

string linha1 = allLines[0];
string linha2 = allLines[1];
string linha3 = allLines[2];
string linha4 = allLines[3];
  • I hadn’t thought of it that way thanks :)

  • @Miuresilva if it worked, don’t forget to mark as answered. Arrange.

1

You first have to select all rows and then by 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 index 2 is used and finally, do not forget to close the file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.