Design a command-line-style file separator with C #

Source: Internet
Author: User
Tags foreach command line file separator readline split visual studio
Command Line | Design Shareware There are many beautiful interface file segmentation procedures, a large file divided into several smaller files, save separately, when needed to merge into the original file, is often a necessary tool.

This article uses the C # language of Microsoft's Visual Studio. NET development environment to also design a file separator and run it as a DOS command line, as simple as using the Copy command. You can also learn about the principle of file segmentation and how the. NET Framework reads and writes files.

Create a new Visual C # project, select Console application, project name is set to Filesplitcon, the system automatically generates Class1.cs files, and the following code is added to the appropriate area of Class1.cs:

Using System;

Using System.Data;

Using System.IO;



Namespace Filesplitcon

{

<summary>

Summary description of the CLASS1.

</summary>

Class Class1

{

<summary>

The main entry point for the application.

</summary>

[STAThread]

static void Main (string[] args)

{

if (args[0]== "s")

{

int i;

FileStream FR = new FileStream (Args[1],filemode.open, FileAccess.Read);



int FileSize = Convert.ToInt32 (Fr. Length)/convert.toint32 (args[2]);



StreamWriter fwlist = new StreamWriter (Args[1] + "." + "list", false);



For (I=1;i<=convert.toint32 (args[2]); i++)

{

byte [] byteread=new byte[filesize];

Fr. Read (byteread,0,filesize);



FileStream FW = new FileStream (args[1] + "." + I, filemode.createnew, FileAccess.Write);

Fwlist. WriteLine (Args[1] + "." + i);

foreach (byte bnext in byteread)

Fw. WriteByte (bnext);

Fw. Close ();

}

if (Fr. Length!=fr. Position)

{

byte [] byteread=new Byte[convert.toint32 (Fr. Length)-filesize* (i-1)];

Fr. Read (Byteread,0,convert.toint32 (Fr. Length)-filesize* (i-1));



FileStream FW = new FileStream (args[1] + "." + I, filemode.createnew, FileAccess.Write);



Fwlist. WriteLine (Args[1] + "." + i);



foreach (byte bnext in byteread)

Fw. WriteByte (bnext);

Fw. Close ();

}

Fr. Close ();

Fwlist. Flush ();

Fwlist. Close ();



Console.WriteLine ("File Split End");

}



if (args[0]== "M")

{

StreamReader frlist=new StreamReader (args[1]+ ".") + "List");



FileStream FW = new FileStream (args[1], filemode.append, FileAccess.Write);



String sline;

Sline=frlist. ReadLine ();



while (Sline!=null)

{

FileStream FR = new FileStream (Sline,filemode.open, FileAccess.Read);



byte [] byteread=new BYTE[FR. Length];

Fr. Read (Byteread,0,convert.toint32 (Fr. Length));



foreach (byte bnext in byteread)

Fw. WriteByte (bnext);

Fr. Close ();

Sline=frlist. ReadLine ();



}

Frlist. Close ();

Fw. Close ();



Console.WriteLine ("End of document merger");



}



}

}

}



After compiling, the executable file is generated under Bin/debug, the size is only 6.5kb, and the execution program is copied to the path that the system can find, such as C:\Winnt (Windows2000 system), making it a system command.

Use the following method at the Command Prompt window (assuming the compiled executable is Filesplitcon.exe):

Split file:

Filesplitcon s to split file file name into several files

For example Filesplitcon S W2ksp3.exe 4 (split w2ksp3.exe into 4 files)



Merging files:

Filesplitcon m merged file name



The code is concise, using the StreamWriter and StreamReader objects, iterating through the files in byte, the first parameter args[0]= "s" to execute the split file code, args[0]= "M" to execute the merged file code.

Description

Split, in the same directory with the split file to generate each split file, file name is the original file name plus. 1,.2,.3, ... To generate a list file. List, record the file name of each split file generated;

For example, for Filesplitcon s W2ksp3.exe 4;

W2ksp3.exe.1

W2ksp3.exe.2

W2ksp3.exe.3

W2ksp3.exe.4

W2ksp3.exe.list

The contents of the W2ksp3.exe.list file are:

W2ksp3.exe.1

W2ksp3.exe.2

W2ksp3.exe.3

W2ksp3.exe.4



When merging, the

W2ksp3.exe.1

W2ksp3.exe.2

W2ksp3.exe.3

W2ksp3.exe.4

W2ksp3.exe.list

5 file copies in any directory, execute command:

Filesplitcon m W2ksp3.exe



Note: If a file cannot be divided into just a few files, the process of the above program is to write the remaining bytes to the new file, so for the above example command, if the W2ksp3.exe can not be divided into 4 files, then will generate 5 files, so the processing should be closer to the actual situation.



The above program is debugged in the Windows2000,visual studio. NET development environment, because the command-line approach is running quickly, dividing a 124M file into 4 files does not feel like waiting time.

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.