How to Merge Text Files in C# .NET Framework
Whether we’re storing data, code, or any other important content in a TXT file, we can merge that content with other text files using a simple programmatic solution.
Using the ready-to-run code below, we can take advantage of a free, low-code API solution to merge two or more TXT files together. The operation will preserve the order of input documents, stacking them vertically in the output TXT file.
To authorize our API calls, we’ll just need a free API key. These will allow a limit of 800 API calls per month, so we can easily carry out hundreds of file merge operations with zero commitments.
To structure our API call, let’s start by installing the SDK. We can run the below command in our Package Manager to install via NuGet:
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2
After that, we can call the below function. Once we copy our API key into the authorization snippet, we can load 10+ TXT files to combine in one API call:
using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class MergeDocumentTxtMultiExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new MergeDocumentApi();
var inputFile1 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | First input file to perform the operation on.
var inputFile2 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Second input file to perform the operation on.
var inputFile3 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Third input file to perform the operation on. (optional)
var inputFile4 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Fourth input file to perform the operation on. (optional)
var inputFile5 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Fifth input file to perform the operation on. (optional)
var inputFile6 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Sixth input file to perform the operation on. (optional)
var inputFile7 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Seventh input file to perform the operation on. (optional)
var inputFile8 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Eighth input file to perform the operation on. (optional)
var inputFile9 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Ninth input file to perform the operation on. (optional)
var inputFile10 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Tenth input file to perform the operation on. (optional)
try
{
// Merge Multple Text (TXT) Files Together
byte[] result = apiInstance.MergeDocumentTxtMulti(inputFile1, inputFile2, inputFile3, inputFile4, inputFile5, inputFile6, inputFile7, inputFile8, inputFile9, inputFile10);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling MergeDocumentApi.MergeDocumentTxtMulti: " + e.Message );
}
}
}
}
Now we can easily & seamlessly merge TXT file content using minimal code.