Copying text from one text file to another

4

I wanted to copy the text from one text file to another with a different name and add one - at the beginning of the line of the text file.

For example:

I have a text file with the following text "0000192" and when copying it to another text file I wanted to get it "-0000192".

But I do not know how I can do this.

I'm creating the first text file like this:

string devolta = Convert.ToString((bsoItemTrans.Transaction.TotalAmount * 100).ToString("-000000000"));
System.IO.File.WriteAllText(@"C:\movedir\SUBTOTALE.txt", devolta);
    
asked by anonymous 19.10.2017 / 17:17

1 answer

4

All you have to do is:

  • Read all rows from the first file and put it in a collection of strings

  • Create a new collection where each row is prefixed with -

  • Save this new collection to a file

  • Code:

    var linhas = File.ReadAllLines("primeiroArquivo.txt"); // Passo 1
    linhas = linhas.Select(l => $"-{l}").ToArray();        // Passo 2
    File.WriteAllLines("novoArquivo.txt", linhas);         // Passo 3
    

    Obviously it can be turned into one-liner :

    File.WriteAllLines("a2.txt", File.ReadAllLines("a1.txt").Select(l => $"-{l}").ToArray());
    
        
    19.10.2017 / 17:26