Iv. Visual C # solution to merge files and specific implementation steps:
Visual C # the idea of merging files is to first obtain the directory where the files to be merged are located, then determine the number of files in the directory, and finally read the files in the order of the file names in the directory by repeating, form a data stream, and use binarywriter to append continuously. After the loop ends, the file is merged. For specific implementation methods, see Step 1 in the following steps. The following describes how to merge files in Visual C:
1. Start Visual Studio. NET.
2. Select File, new, and project. The new project dialog box is displayed.
3. Set project type to Visual C # project ].
4. Set template to Windows application ].
5. In the Name text box, enter merge program ].
6. in the location text box, enter E: \.. Net project, and then click OK.. Net project "directory generates a folder named" merge program "and creates a project file named" merge program. 04:
Figure 04: [merge program] project-[new project] dialog box |
7. set Visual Studio. switch the current window of net to the form1.cs window, drag the following components to form1 form from the Windows Forms components tab in the toolbox, and perform the corresponding operations:
An openfiledialog component used to select a merged file.
A progressbar component used to display the progress of file merging.
The two textbox components are used to display the selected files of the openfiledialog component and the names of the merged large files.
Two label components.
The two buttons are used to select a merged file and merge all the files in the selected directory. After the two buttons are dragged into the form1 design form, double-click them, the system generates the processing code corresponding to the click events of the two components in the form1.cs file.
13. Switch the current window of Visual Studio. NET to the form1.cs window, and adjust the main attributes of the component according to the values shown in Table 06:
Component Type |
Component name |
Attribute |
Setting result |
Button |
Button1 |
Flatstyle |
Flat |
Button1 |
Text |
Run |
Button2 |
Flatstyle |
Popup |
Button2 |
Text |
... |
Form |
Form1 |
Text |
Merge program |
Form1 |
Maximizebox |
False |
Form1 |
Minimizebox |
True |
Form1 |
Formborderstyle |
Formborderstyle. fixedsingle |
Label |
Label1 |
Text |
Select the size: |
Label2 |
Text |
Select File: |
Textbox |
Textbox1 |
Text |
|
Textbox2 |
Text |
|
Table 06: Key Component Attribute setting value table in [file segmentation]
Now, the interface design and functional implementation preparations of the [merge program] project are completed, as shown in Figure 05 after the design:
Figure 05: [merge program] project design-based interface |
8. In the Solution Explorer window, double-click the form1.cs file To Go To The form1.cs file editing page. At the beginning of the form1.cs file, use the following imported namespace code to replace the default imported namespace code.
Using system; Using system. drawing; Using system. collections; Using system. componentmodel; Using system. Windows. forms; Using system. Data; Using system. IO; Using system. text; Using system. Data. oledb; |
9. Add the following code in the from1.cs class code area. The following Code defines a string global variable to store the directory of the merged file:
Public String sdirectoryname = ""; // Define a global variable to store the directory of the merged File |
10. Replace the processing code corresponding to the click event of button2 in form1.cs with the following code. The following code function is to obtain the directory of the file based on the file selection dialog box and prepare for the subsequent file merging:
Private void button2_click (Object sender, system. 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 } |
11. Replace the processing code corresponding to the click event of button1 in form1.cs with the following code. The following code function is to merge all files in the directory where the obtained files are located and generate the merged files:
Private void button#click (Object sender, system. 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 ); // Initialize the binarywriter using the 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 based on the file name and open mode corresponding to the small file, which is used for reading and splitting Binaryreader tempreader = new binaryreader (tempstream ); // Use the 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 (); // Close the filestream file stream Progressbar1.value = I + 1; // Display the merging process } Addwriter. Close (); // Close the binarywriter file writer Addstream. Close (); // Close the filestream file stream MessageBox. Show ("merged successfully! "); Progressbar1.value = 0; } |
12. After the above steps are completed correctly, all the work of the [merge program] project is basically completed. Click the shortcut key F5 to run the program and click 【...] Click the button to select any file in the directory where the split file is located, enter the name of the merged file, and click the execute button to select all files in the merged directory, and kill the input merged files. Figure 06 shows the running interface of the merge program project:
Figure 06: [merge program] project running interface |
V. Summary:
Visual C # has a strong file processing capability because many File Processing classes are provided in. NET Framework SDK. Splitting a large file program and merging a small file program should be a complicated application in file programming. However, through the introduction in this article, it can be seen that the filestream, binaryreader, and binarywriter classes are basically completed, and there are not many codes. In fact, to master the processing of files in Visual C #, these three classes are the key, the key, and the difficulty, because other file processing classes are either similar to the usage of these three classes, the structure is simple and easy to master. Finally, I hope this article will help you understand Visual C # And file programming that is proficient in Visual C.