My system generates a text file, and based on the sum of one information in each line, it calculates a check digit.
However, this result should be in the first line of the file, how can I include this line at the beginning?
My system generates a text file, and based on the sum of one information in each line, it calculates a check digit.
However, this result should be in the first line of the file, how can I include this line at the beginning?
Simple. You only need
Read the entire file and save it to a list of string
, where each line represents a string in the list
Insert the new line in position 0 of the list
Write all strings
of the list in the file
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