Introduction
I have used FileStream class for merging two files. The first file will be opened for appending. The second file will be opened and read into a byte array. Then this byte array is written onto the first file stream and then both file steams will be closed. Now you have successfully appended the first file with the second file.
Uses
If are left with two parts of a video or audio file you have no option to play it because the player may not be able to read partial files. This is useful for joining files of the same format split due to some size restrictions.
Code
private void cmdMerge_Click(object sender, EventArgs e)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
{
string sFile1 = txtFile1.Text;
string sFile2 = txtFile2.Text;
FileStream fs1=null;
FileStream fs2=null;
try
{
fs1 = File.Open(sFile1, FileMode.Append);
fs2 = File.Open(sFile2, FileMode.Open);
byte[] fs2Content = new byte[fs2.Length];
fs2.Read(fs2Content, 0, (int)fs2.Length);
fs1.Write(fs2Content, 0, (int)fs2.Length);
MessageBox.Show("Done!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + " : " + ex.StackTrace);
}
finally
{
fs1.Close();
fs2.Close();
}
}