How to copy text from one text file to another

Asked

Viewed 416 times

4

I wanted to copy the text from one text file to another with a different name and add a - 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 to another text file I wanted it to look like this "-0000192".

But I don’t know how I can do it .

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);

1 answer

4


All you need to do is:

  1. Read all lines from the first file and put in a collection of strings

  2. Create a new collection where each row is prefixed with -

  3. Save this new collection to an archive

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 transformed into one-Liner:

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

Browser other questions tagged

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