How to merge file with different extensions in C#

yashrajsinh gohil 0 Reputation points
2024-03-05T04:15:57.2933333+00:00

Merging Files having different extensions into one given file with .mrg extension

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,649 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2024-03-14T12:55:08.5666667+00:00

    Hi @yashrajsinh gohil ,Welcome to Microsoft Q&A,

    What you are looking for is pretty much what Mamu Cacga said.

    Using FileStream, for each extension, the program loops through the list of files found. For each file, it uses FileStream** to open the file and copies its contents into the merged file, using the CopyTo method.

    Once all files have been merged into a single file, the using statement ensures that all open file streams are properly closed and resources released.

    The extension of the merged file is hard-coded in the variable, so you will need to try it yourself for the files generated by this method.

    using System;
    using System.IO;
    
    class Program
    {
         static void Main()
         {
             //Set the folder path to be merged
             string folderPath = @"C:\Path\To\Your\Folder";
    
             //Set the extension of the files to be merged
             string[] fileExtensions = { ".txt", ".doc", ".pdf" }; // Add more extensions as needed
    
             //Set the path of the merged file to be generated
             string mergedFilePath = @"C:\Path\To\Your\Folder\merged.mrg";
    
             // merge files
             MergeFiles(folderPath, fileExtensions, mergedFilePath);
    
             Console.WriteLine("Files successfully merged!");
         }
    
         static void MergeFiles(string folderPath, string[] fileExtensions, string mergedFilePath)
         {
             //Create new merged file
             using (FileStream mergedFileStream = new FileStream(mergedFilePath, FileMode.Create))
             {
                 foreach (string fileExtension in fileExtensions)
                 {
                     // Get all files with the current extension
                     string[] files = Directory.GetFiles(folderPath, "*" + fileExtension);
    
                     foreach (string file in files)
                     {
                         // Open the current file and write its contents to the merged file
                         using (FileStream fileStream = new FileStream(file, FileMode.Open))
                         {
                             fileStream.CopyTo(mergedFileStream);
                         }
                     }
                 }
             }
         }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments