How to Merge Files using C# VB.Net

Few days ago I have a requirement to make a processing engine which will download, decode & staging almost 70k+ ASN files. So that I have developed an ASN Decoder using Turbo C. Then make a DLL to use this decoder in my C# project. Everything is good but one problem is that my processing exe will take almost 6 hours to process all files. After reviewing my source code I found that each time my ASN Decoder DLL open a single file then decode it then close the file & also create a csv file for bulk insert into my database. I took a decision to reduce the time for open & closing a file. How i can do it? I made it by merging mutiple files in a bunch in a single file. So that my DLL need to open almost 1/3rd of files than earlier & my procesiing now takes only 2 hours. If you are facing such type of problem then you can follow my way.

C# Code to Merge Files:

        private void Form1_Load(object sender, EventArgs e)
            string sFile1 = @"File1.txt";
            string sFile2 = @"File2.txt";
            string sFile3 = @"File3.txt";

            FileStream FStream1 = null;
            FileStream FStream2 = null;
            FileStream FStream3 = null;

            FStream1 = File.Open(sFile1, FileMode.Open);
            FStream2 = File.Open(sFile2, FileMode.Open);
            FStream3 = File.Open(sFile3, FileMode.Append);

            byte[] FStream1Content = new byte[FStream1.Length];
            byte[] FStream2Content = new byte[FStream2.Length];
            FStream1.Read(FStream1Content, 0, (int)FStream1.Length);
            FStream2.Read(FStream2Content, 0, (int)FStream2.Length);

            FStream3.Write(FStream1Content, 0, (int)FStream1.Length);
            FStream3.Write(FStream2Content, 0, (int)FStream2.Length);

            MessageBox.Show("Merging Successfully Completed!");


Note: Don’t forget to add “System.IO” namespace.

VB.Net Code to Merge Files:

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim sFile1 As String = "File1.txt"
        Dim sFile2 As String = "File2.txt"
        Dim sFile3 As String = "File3.txt"

        Dim FStream1 As FileStream = Nothing
        Dim FStream2 As FileStream = Nothing
        Dim FStream3 As FileStream = Nothing

        FStream1 = File.Open(sFile1, FileMode.Open)
        FStream2 = File.Open(sFile2, FileMode.Open)
        FStream3 = File.Open(sFile3, FileMode.Append)

        Dim FStream1Content As Byte() = New Byte(FStream1.Length - 1) {}
        Dim FStream2Content As Byte() = New Byte(FStream2.Length - 1) {}

        FStream1.Read(FStream1Content, 0, CInt(FStream1.Length))
        FStream2.Read(FStream2Content, 0, CInt(FStream2.Length))

        FStream3.Write(FStream1Content, 0, CInt(FStream1.Length))
        FStream3.Write(FStream2Content, 0, CInt(FStream2.Length))

        MessageBox.Show("Merging Successfully Completed!")


    End Sub

Happy coding !!

Download Code Example C#        Download Code Example VB.Net

Posted in C#, VB.Net
2 comments on “How to Merge Files using C# VB.Net
  1. pedro says:

    how could you do the same think with multiple file :/ i try to do this File 1 0 1 2 File xx 2 3 6 I want one file 0 .. 2 1 .. 3 2 .. 6 I don’t have each time the same number or file

  2. Shawpnendu Bikash says:

    For Multiple Files to merge you can follow the below code example:
    private void Form1_Load(object sender, EventArgs e)

    string[] Files = Directory.GetFiles(“C:\\airtemp\\”, “*.txt”);
    foreach (string filename in Files)
    FileStream FStreamFinal = File.Open(“C:\\airtemp\\Final.txt”, FileMode.Append);
    FileStream FStream1 = File.Open(filename, FileMode.Open);
    byte[] FStream1Content = new byte[FStream1.Length];
    FStream1.Read(FStream1Content, 0, (int)FStream1.Length);
    FStreamFinal.Write(FStream1Content, 0, (int)FStream1.Length);

Leave a Reply

Your email address will not be published. Required fields are marked *