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();
}
}
Sandral BullockkPosted Jul 8, 2014, 4:50 PM
that code sucks.. it is good for text files, not for video file. Tested with MP4 format.. does not even work. Post some useful stuff due... dont waste the time of others
swati srivastavaPosted Apr 12, 2011, 6:47 AM
i tried the code and tried to merge two different types of video files but couldnt get +ve result
Jovit PeridoeditedPosted Mar 1, 2010, 10:51 PMEdited Mar 1, 2010, 11:00 PM
hello, i'm a student studying C# because of our final project..can anyone help me please about merging three text files which are from saved points gathered from zedgraph to be separated by comma.. for example file1.txt contains 1 2 3 file2.txt contains 4 5 6 file3.txt contains 7 8 9 through which i need to have an output of 1 , 4 , 7 2 , 5 , 8 3 , 6 , 9 ...thank you in advance.. this is very urgent and important..if you have any codes available..tnx.
gautam peditedPosted Feb 7, 2010, 11:33 PMEdited Feb 7, 2010, 11:35 PM
i am gautam i want to cut and join avi video file using c#.net 2005 plz help me. thanks email [email protected]
Niki SommerfeldPosted May 29, 2009, 4:26 PM
I have been struggling with this all day today and just when I was about to give up and try again tomorrow, I found your code. Thank you so much for sharing your knowledge with the rest of us.
Angel ArnaudovPosted Aug 28, 2008, 3:45 AM
Hi there! I want to know if is possible to save and view the file after the two files are merged in one? Thx ahead
Angel ArnaudovPosted Aug 28, 2008, 3:45 AM
Hi there! I want to know if is possible to save and view the file after the two files are merged in one? Thx ahead
Angel ArnaudovPosted Aug 28, 2008, 3:45 AM
Hi there! I want to know if is possible to save and view the file after the two files are merged in one? Thx ahead
Angel ArnaudovPosted Aug 28, 2008, 3:45 AM
Hi there! I want to know if is possible to save and view the file after the two files are merged in one? Thx ahead