C # file splitting and merging Part 1 from the Internet)

Source: Internet
Author: User

This article briefly introduces how to use C # to merge and split files. In my project, I need to use the functions of this module, so I will post this article after practice, for backup:

First, let's split the file first. We created a new winform project. The project design is as follows:

The following controls are available:

An openfiledialog component used to select the large file to be split.

A progressbar component used to display the progress of file splitting.

A ComboBox component used to select the file split size.

The two textbox components are used to display the directories of files selected by the openfiledialog component and small files separated by input.

Four label components.
Then, add the following button ...:CodeOf course, you still need using system. IO;

 

 Browse files 
Openfiledialog1.title = " Select the name of the file to be split " ;
Dialogresult drtemp = Openfiledialog1.showdialog ();
If (Drtemp = Dialogresult. OK && Openfiledialog1.filename ! = String . Empty)
{
Textbox1.text = Openfiledialog1.filename;
Button2.enabled = True ;
}

Then add the following code to the execution button:

 

 Execution button 
Private Void Button2_click ( Object Sender, eventargs E)
{
Int Ifilesize = Int32.parse (combobox1.text) * 1024 ;
// Set the size of the split small file according to the selection.
If (Directory. exists (textbox2.text ))
Directory. Delete (textbox2.text, True );
// If the computer has a directory for storing split files, delete all files in this directory.
Else
Directory. createdirectory (textbox2.text );
// Otherwise, create a directory on the computer.
Filestream splitfilestream = New Filestream (textbox1.text, filemode. Open );
// Initialize the filestream file stream instance in the full path corresponding string and file opening mode.
Binaryreader splitfilereader = New Binaryreader (splitfilestream );
// Initialize binaryreader with filestream file stream
Byte [] Tempbytes;
// Maximum data read by each split
Int Ifilecount = ( Int ) (Splitfilestream. Length / Ifilesize );
// Total number of small files
Progressbar1.maximum = Ifilecount;
If (Splitfilestream. Length % Ifilesize ! = 0 ) Ifilecount ++ ;
String [] Tempextra = Textbox1.text. Split ( ' . ' );
/* Split large files into multiple small files in a loop */
For ( Int I = 1 ; I <= Ifilecount; I ++ )
{
String Stempfilename = Textbox2.text + @" \ " + I. tostring (). padleft ( 4 , ' 0 ' ) + " . " + Tempextra [tempextra. Length - 1 ]; // Small file name
// Determine the file name of the small file
Filestream tempstream = New Filestream (stempfilename, filemode. openorcreate );
// Initialize the filestream file stream instance based on the file name and file opening mode.
Binarywriter tempwriter = New Binarywriter (tempstream );
// Create and initialize a binarywriter instance using a filestream instance
Tempbytes = Splitfilereader. readbytes (ifilesize );
// Read data of a specified size from a large file
Tempwriter. Write (tempbytes );
// Write this data into a small file
Tempwriter. Close ();
// Close the writer to form a small file
Tempstream. Close ();
// Close file stream
Progressbar1.value = I - 1 ;
}
Splitfilereader. Close ();
// Close large file Reader
Splitfilestream. Close ();
MessageBox. Show ( " Split successfully! " );
Progressbar1.value = 0 ;

}

Then, set the combox item and add the 720, 1440, and 2880 items.

Compile and run the following code:

Then look at the input directory, the following files will appear, my source file has 16 m, so there are many small files.

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.