C # file splitting and merging Part 2

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.