C# 文件拆分器

组合时采用了两层的COPY命令,可多组合一些文件,其实用测试命令行总长度的办法可以理论上实现无限拆分文件的组合,但实用价值就不高了,拆成万余份文件不但此单线程方法显得效率低下,而且应当用更优秀算法进行分割和组合。

这个程序最终只能归为“玩具”一类

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

namespace filesplit
{
///

1<summary>   
2/// Form1 的摘要说明。   
3/// </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;
///

1<summary>   
2/// 必需的设计器变量。   
3/// </summary>

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

///

1<summary>   
2/// 清理所有正在使用的资源。   
3/// </summary>

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
///

1<summary>   
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
3/// 此方法的内容。   
4/// </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 = "拆分";
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 = "浏览";
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 = "尺寸";
//
// 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

///

1<summary>   
2/// 应用程序的主入口点。   
3/// </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("输入拆分数字不正确");
return;
}

#region "分配文件夹"
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

 1<l;) !="tempcount" "="" ");="" "+for2);="" "+getfilename(ofd.filename));="" "temp"="" %="" &&="" ((for2="" ((int)(tempcount++)).tostring());="" (for2="" (int="" );="" +="" 1="" 100)="0)" ;="" <="" \\\"+filecount.tostring(),system.io.filemode.createnew="" b");="" catch(system.exception="" ee)="" else="" else{="" f.close();="" f2="new" f2.close();="" f2.writebyte(r.readbyte());="" filecount++;="" filecount-="1;" firstname="0;" for="" for(int="" for1="0;for1&lt;i;for1++)" for2="1;for2&lt;filecount;for2++)" for2++)="" for2.tostring()="" for2.tostring());="" gc.collect();="" getfilename(string="" i="str.Length-1;i" if="" if(for2+1!="filecount)" if(x<l)="" int="" off");="" private="" result="" str)="" string="" system.io.filestream="" system.io.filestream(workpath+"="" system.io.streamwriter="" system.io.streamwriter(system.io.file.open(workpath+"\\\"+"组合文件.bat",system.io.filemode.create),system.text.encoding.default);="" system.windows.forms.messagebox.show("拆分文件创建已经完成,共拆分为"+filecount.tostring()+"部分");="" system.windows.forms.messagebox.show("操作出现失误,没有完成拆分:"+ee.message="" t="new" t.close();="" t.write("="" t.write("copy="" t.write("temp"="" t.write(for2.tostring()+"="" t.writeline("@echo="" t.writeline("del="" t.writeline();="" tempcount="0;" tempcount!="0)" tempcount;="" x++;="" {="" }="">=0;i--)   
 2{   
 3if(str[i]=='\\\')   
 4{   
 5firstname=i;   
 6break;   
 7}   
 8}   
 9for(int i=firstname+1;i&lt;str.Length;i++)   
10{   
11result+=str[i]; 
12
13}   
14return result;   
15} 
16
17  
18  
19}   
20}</l;)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus