This is the merging part.ArticleSplit files are merged to create a new project. Compared with the previous project, a combox control is missing. Because we want to merge files, we do not need to select the file size, the design diagram is as follows:
Similarly, reference system. Io and add the following to the browser button:Code:
Browse
Private Void Button#click ( Object Sender, eventargs E)
{
Openfiledialog1.title = " Select the first file to be merged " ;
System. Windows. Forms. dialogresult drtemp = Openfiledialog1.showdialog ();
If (Drtemp = Dialogresult. OK && Openfiledialog1.filename ! = "" )
{
Textbox1.text = Openfiledialog1.filename;
}
String [] Path = Openfiledialog1.filename. Split ( @" \ " . Tochararray ());
String Stemp = "" ;
Int I = 0 ;
For (I = 0 ; I < Path. Length - 1 ; I ++ )
{
Stemp = Stemp + Path [I] + @" \ " ;
}
Button1.enabled = True ;
Sdirectoryname = Stemp;
// Obtain the directory where the file is located
}
Add the following code to button2:
Merge
Private Void Button2_click ( Object Sender, eventargs E)
{
String [] Arrfilenames = Directory. getfiles (sdirectoryname );
// Obtain all the small files in the directory where the split small files are stored.
Int Isumfile = Arrfilenames. length;
Progressbar1.maximum = Isumfile;
Filestream addstream = New Filestream (textbox2.text, filemode. openorcreate );
// Create and initialize the filestream file stream with the merged file name and Opening Method
Binarywriter addwriter = New Binarywriter (addstream );
// Binarywriter writer is initialized using a filestream file stream, which is used to merge split files.
/* Merge small files cyclically and generate merged files */
For ( Int I = 0 ; I < Isumfile; I ++ )
{
Filestream tempstream = New Filestream (arrfilenames [I], filemode. Open );
// Initialize the filestream file stream with the file name and open mode corresponding to the small file, which is used for reading and splitting.
Binaryreader tempreader = New Binaryreader (tempstream );
// Use filestream file stream to initialize the binaryreader file reader. It also reads the split file.
Addwriter. Write (tempreader. readbytes (( Int ) Tempstream. Length ));
// Read the data in the split file and generate the merged File
Tempreader. Close ();
// Disable binaryreader
Tempstream. Close ();
// Disable filestream file streams
Progressbar1.value = I + 1 ;
// Display merging process
}
Addwriter. Close ();
// Disable the binarywriter file writer
Addstream. Close ();
// Disable filestream file streams
MessageBox. Show ( " Merged successfully! " );
Progressbar1.value = 0 ;
}
Compile and run the program. Find the first file, select 0001.rar, and merge it.
You Can See my.rar in the output file.