程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> C# 文件拆分器

C# 文件拆分器

編輯:.NET實例教程

組合時采用了兩層的COPY命令,可多組合一些文件,其實用測試命令行總長度的辦法可以理論上實現無限拆分文件的組合,但實用價值就不高了,拆成萬余份文件不但此單線程方法顯得效率低下,而且應當用更優秀算法進行分割和組合。

這個程序最終只能歸為“玩具”一類。

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

namespace filesplit
{
/// <summary>
/// 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>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}

/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </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

/// <summary>
/// 應用程序的主入口點。
/// </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<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+"\\"+"組合文件.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("拆分文件創建已經完成,共拆分為"+filecount.ToString()+"部分");
}
catch(System.Exception ee)
{
System.Windows.Forms.MessageBox.Show("操作出現失誤,沒有完成拆分:"+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;
}



}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved