How do I include a line at the beginning of a text file? [closed]

1

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?

    
asked by anonymous 28.08.2017 / 16:00

1 answer

2

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
    
        
    28.08.2017 / 16:03