How to include a line at the beginning of a text file?

Asked

Viewed 797 times

1

My system generates a text file, and based on the sum of an information that appears on each line, it makes the calculation of a checker digit.

However, this result should be in the first line of the file, as I can do to include this line at the beginning?

1 answer

2


Simple. You just need

  1. Read the entire file and save to a list of string, where each row represents a string in the list

  2. Insert new row at position 0 of the list

  3. Write all the strings of the list in the archive

const string nomeArquivo = "arquivo.txt";

List<string> linhas = File.ReadAllLines(nomeArquivo).ToList(); // Passo 1
linhas.Insert(0, "Primeira linha"); // Passo 2
File.WriteAllLines(nomeArquivo, linhas); // Passo 3

Browser other questions tagged

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