concatenate files in c#

Asked

Viewed 465 times

2

Good morning

I have several txt files in the same directory milestones-1, milestones-2..., only that I need to create a file only. but with an aggravation that each file will be a column of the final file. as if it were an array. where each file is matrix column.

Someone could help me

  • 1

    You need to give us more information so that we can try to respond. Anyway, did you do something? Are you having a specific problem?

1 answer

2

The performative way is like this:

const int chunkSize = 2 * 1024; // 2KB
var inputFiles = new[] { "marcos1.txt", "marcos2.txt", "marcos3.txt" };
using (var output = File.Create("marcos-juntos.txt"))
{
    foreach (var file in inputFiles)
    {
        using (var input = File.OpenRead(file))
        {
            var buffer = new byte[chunkSize];
            int bytesRead;
            while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, bytesRead);
            }
        }
    }
}

I took it from here.

Browser other questions tagged

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