C # File Splitter

Source: Internet
Author: User
Tags command line count split tostring
The two-level copy command is used in the combination. Can be combined with a number of files, in fact, the total length of the test command line can theoretically achieve a combination of infinite split file, but the practical value is not high, millions more than a single document This single-threaded method appears inefficient, and should use more excellent algorithm for segmentation and combination.

This procedure can only be categorized as "toys".

Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;

Namespace Filesplit
{
<summary>
Summary description of the Form1.
</summary>
public class Form1:System.Windows.Forms.Form
{
Private System.Windows.Forms.TextBox TextBox1;
Private System.Windows.Forms.TextBox TextBox2;
Private System.Windows.Forms.Button button1;
Private System.Windows.Forms.Button button2;
Private System.Windows.Forms.OpenFileDialog OFD;
Private System.Windows.Forms.Label Label1;
Private System.Windows.Forms.Label Label2;
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent call
//
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This.textbox1 = new System.Windows.Forms.TextBox ();
This.textbox2 = new System.Windows.Forms.TextBox ();
This.button1 = new System.Windows.Forms.Button ();
This.button2 = new System.Windows.Forms.Button ();
THIS.OFD = new System.Windows.Forms.OpenFileDialog ();
This.label1 = new System.Windows.Forms.Label ();
This.label2 = new System.Windows.Forms.Label ();
This. SuspendLayout ();
//
TextBox1
//
This.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
This.textBox1.Location = new System.Drawing.Point (16, 16);
This.textBox1.Name = "TextBox1";
This.textBox1.ReadOnly = true;
This.textBox1.Size = new System.Drawing.Size (376, 21);
This.textBox1.TabIndex = 0;
This.textBox1.Text = "File Path";
//
TextBox2
//
This.textBox2.Location = new System.Drawing.Point (56, 48);
This.textBox2.Name = "TextBox2";
This.textBox2.Size = new System.Drawing.Size (72, 21);
This.textBox2.TabIndex = 1;
This.textBox2.Text = "";
//
Button1
//
This.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
This.button1.Location = new System.Drawing.Point (200, 48);
This.button1.Name = "Button1";
This.button1.Size = new System.Drawing.Size (272, 24);
This.button1.TabIndex = 2;
This.button1.Text = "Split";
This.button1.Click + = new System.EventHandler (This.button1_click);
//
Button2
//
This.button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
This.button2.Location = new System.Drawing.Point (400, 16);
This.button2.Name = "Button2";
This.button2.Size = new System.Drawing.Size (72, 24);
This.button2.TabIndex = 3;
This.button2.Text = "browsing";
This.button2.Click + = new System.EventHandler (This.button2_click);
//
Label1
//
This.label1.Location = new System.Drawing.Point (16, 48);
This.label1.Name = "Label1";
This.label1.Size = new System.Drawing.Size (32, 23);
This.label1.TabIndex = 4;
This.label1.Text = "Size";
//
Label2
//
This.label2.Location = new System.Drawing.Point (136, 48);
This.label2.Name = "Label2";
This.label2.Size = new System.Drawing.Size (56, 16);
This.label2.TabIndex = 5;
This.label2.Text = "KB";
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (498, 79);
This. Controls.Add (THIS.LABEL2);
This. Controls.Add (THIS.LABEL1);
This. Controls.Add (This.button2);
This. Controls.Add (This.button1);
This. Controls.Add (THIS.TEXTBOX2);
This. Controls.Add (This.textbox1);
This. FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
This. MaximizeBox = false;
This. MinimizeBox = false;
This. Name = "Form1";
This. Text = "Spliter";
This. ResumeLayout (FALSE);

}
#endregion

<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}


private void Button2_Click (object sender, System.EventArgs e)
{
if (This.ofd.ShowDialog ()!=system.windows.forms.dialogresult.ok) {return;}
This.textbox1.text=ofd. FileName;
}

private void Button1_Click (object sender, System.EventArgs e)
{


Try
{

int i=1;
Try
{
I=int. Parse (This.textBox2.Text);
if (I <= 0) {System.Exception ee = new Exception (); throw EE;}
i*=1000;
}
Catch
{
i=0;
System.Windows.Forms.MessageBox.Show ("Input split number is incorrect");
Return
}

#region "Assign Folder"
int count=1;
String WORKPATH=OFD. Filename.tostring () + "Split";
while (true)
{

if (! System.IO.Directory.Exists (Workpath)) {break;}
Else
{
if (! System.IO.Directory.Exists (workpath+ "_" +count. ToString ()))
{
workpath+= "_" +count. ToString ();
Break
}
Else
{
count++;
}

}
}
System.IO.Directory.CreateDirectory (Workpath);
#endregion


System.IO.FileStream f=new System.IO.FileStream (ofd. Filename,system.io.filemode.open);

System.IO.BinaryReader r = new System.IO.BinaryReader (f);

Long L=f.length;long x=0;
int filecount=1;


for (;x<l;)
{
System.IO.FileStream f2=new System.IO.FileStream (workpath+ "\" +filecount.) ToString (), System.IO.FileMode.CreateNew);
for (int for1=0;for1<i;for1++)
{
if (x<l)
{
F2. WriteByte (R.readbyte ());

}
x + +;
}
F2. Close ();
filecount++;
}
F.close ();

System.IO.StreamWriter T =new System.IO.StreamWriter (System.IO.File.Open (workpath+ "\" + "Combo file. Bat", System.IO.FileMode.Create), System.Text.Encoding.Default);

T.writeline ("@echo off");
T.write ("copy");

int tempcount = 0;



for (int for2=1;for2<filecount;for2++)
{
T.write (For2. ToString () + "/b");
if (For2+1!=filecount)
{
if ((for2% 100) = = 0)
{
T.write ("" + "temp" + ((int) (tempcount++)). ToString ());
T.writeline ();
T.write ("copy");
}
Else
{
T.write ("+");
}
}
else{
T.write ("" + "temp" + ((int) (tempcount++)). ToString ());
T.writeline ();
}

}

T.write ("copy");
for (int for2 = 0; For2 < tempcount; for2++)
{
T.write ("temp" + For2). ToString () + "/b");
if (For2 + 1!= tempcount && tempcount!=0)
{
T.write ("+");
}

else{
T.write ("" +getfilename) (OFD. FileName));
T.writeline ();
}


}




for (int for2 = 0; For2 < tempcount; for2++)
{
T.writeline ("del" + "temp" + For2). ToString ());
}

for (int for2=1;for2<filecount;for2++)
{
T.writeline ("del" +for2);
}




T.close ();
Filecount-=1;

System.Windows.Forms.MessageBox.Show ("Split file creation has been completed and split into" +filecount.) ToString () + "part");
}
catch (System.Exception ee)
{
System.Windows.Forms.MessageBox.Show ("Operation error, did not complete split:" +ee. message);
Gc. Collect ();
}
}




private string GetFileName (String str)
{
int firstname=0;
String result= "";
for (int i=str. length-1;i>=0;i--)
{
if (str[i]== ' \ ")
{
Firstname=i;
Break
}
}
for (int i=firstname+1;i<str. length;i++)
{
Result+=str[i];

}
return result;
}



}
}





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.