.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Converting multiple files to single files

Posted By:nishithraj       Posted Date: December 24, 2009    Points: 10    Category: File Operation    URL: http://www.dotnetspark.com  

Many times we may require to convert multiple files into single file.

We can achieve this by Csharp. We need to implement the below code for that.


private void ConcatenateFiles(string strDestinationFileName, string strHeaderText, params string[] sourceFiles) 
    System.IO.StreamWriter swFileWriter = default(System.IO.StreamWriter);
    System.IO.StreamReader stFileReader = default(System.IO.StreamReader);
    string strSourceFile = null;
    try {
        //Create the desitnation file
        if (File.Exists(strDestinationFileName) == true) {
        swFileWriter = new System.IO.StreamWriter(strDestinationFileName, true);
        foreach (var strSourceFile in sourceFiles) {
            //Write the header string on the top of each file contents
            try {
                //Open the source file in read mode
                stFileReader = new System.IO.StreamReader(strSourceFile);
                //Append content of the current file in the destination file
                swFileWriter.Write(stFileReader.ReadToEnd() + Environment.NewLine);
            finally {
                if ((stFileReader != null)) {
                    stFileReader = null;
    catch (Exception ex) {
    finally {
        //Handle Exception here
        //Close swFileWriter stream
        if ((swFileWriter != null)) {
            swFileWriter = null;


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend