Thursday, 20 July 2017

C# Merge Splitted File

Hi all, here a very easy lwebcode free script to split file in C#

// calls example
...

int iSplittedFileSize = 1024 * 8;
MergeFile(@"C:\script\", iSplittedFileSize, @"C:\LwebCodeMergeFile\");

...

public static void MergeFile(string inputfoldername1, string SaveFileFolder)
        {
            string[] tmpfiles = Directory.GetFiles(inputfoldername1, "*.tmp");

            FileStream outPutFile = null;
            string PrevFileName = "";

            foreach (string tempFile in tmpfiles)
            {
                string fileName = Path.GetFileNameWithoutExtension(tempFile);
                string baseFileName = fileName.Substring(0, fileName.IndexOf(Convert.ToChar(".")));
                string extension = Path.GetExtension(fileName);

                if (!PrevFileName.Equals(baseFileName))
                {
                    if (outPutFile != null)
                    {
                        outPutFile.Flush();
                        outPutFile.Close();
                    }
                    outPutFile = new FileStream(SaveFileFolder + "\\" + baseFileName + extension, FileMode.OpenOrCreate, FileAccess.Write);

                }

                int bytesRead = 0;
                byte[] buffer = new byte[1024];
                FileStream inputTempFile = new FileStream(tempFile, FileMode.OpenOrCreate, FileAccess.Read);

                while ((bytesRead = inputTempFile.Read(buffer, 0, 1024)) > 0)
                    outPutFile.Write(buffer, 0, bytesRead);

                inputTempFile.Close();
               
                PrevFileName = baseFileName;

            }
        }


Hope you like it ;)

1 comment:

  1. It was very encouraging to see this kind of content. Thank you for sharing.Best Online Marketing

    ReplyDelete