C# 文件拆分器

组合时采用了两层的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;
}

}
}

时间: 2024-08-01 19:31:46

C# 文件拆分器的相关文章

用C#设计一个命令行方式的文件分割器

命令行|设计 共享软件中有很多界面美观的文件分割程序,将一个大文件分割成几个较小的文件,分别保存,需要的时候再合并成原来的文件,常常是必备的工具. 本文用微软的visual studio .net开发环境的C#语言也来设计一个文件分割器,并且以DOS命令行方式运行,就象使用copy命令一样简单.从中也可以了解文件分割的原理以及.net框架对文件的读写操作方式. 新建一个visual c#项目,选择"控制台应用程序",项目名称设置为filesplitcon,系统自动产生了class1.c

Windows 8开发入门(十七) 文件选择器 文件保存器 文件夹选择器

在Windows 8中的文件选择器相比windows之前的几个版本有很大的不同,在本文中也将讲解如何使用 windows文件选择器进行单选和多选文件,另外也要看看文件保存器和文件夹选择器. 在这之前我们需 要了解这三种选择器在Windows 8中所使用的类如下: 文件选择器:FileOpenPicker 常用属性 和方法:SuggestedStartLocation-选择器的初始位置 CommitButtonText-提交按钮的文字 FileTypeFilter-设置可选择的文件类型 ViewM

API文件产生器-javadoc.exe

Javadoc API文件产生器 Javadoc程序读取一个Java类文件并自动创建一组HTML文件,这些HTML 文件描述了Java类文件的类.变量.成员函数,所有Java类库的APIHTML 文件都可以由此程序创建.Javadoc把软件包名或源文件列表当做一个变量.Javadoc依靠以@打头的备注标记来创建HTML文件,下面就是标注的列表,它们被Javadoc用于在HTML 文件中创建链接. 选项 功能 @see classname 此标注在类列表中增加一个到所提供类的"See Also&q

【玩转.Net MF – 03】远程文件查看器

虽说目前.Net Micro Framework已经支持文件系统(FAT16/FAT32),但在远 程还无法直接访问,从某种意义上讲,无法和PC交互的存储介质显得有些鸡肋. 我做SideShow相关开发的时候,为了向该文件系统拷贝文件,实现了 UsbMassStorage功能,把设备当优盘来用,但这样做,等于独占了USB口,并且设 备和PC的连接也必须为USB,对仅拥有串口或网口的设备是无效的.做过WinCE或 Windows Mobile开发的人都知道,VS2008开发工具提供了些远程工具,诸

从零开始玩编程系列:文件捆绑器

  将多个文件合并成一个最终可执行文件,运行这个最终合成文件后,就相当于运行了合并前的多个文件,这种程序在木马或后门程序合并中会经常用到,你想知道它是怎么用程序实现的么?下面我就用VC6做的一个文件捆绑器的例子来告诉你. 其实文件捆绑器的构成思想非常简单:合并文件时,建立一个新的二进制文件,先写入你的自身捆绑程序的数据和文件长度,再写入你要捆绑的第一个文件的数据和其文件长度,跟着写入你要捆绑的第二个文件的数据和文件长度--最后直接写入你要捆绑的最后一个文件的数据(不需其文件长度).分解释放最终合

怎么让Windows8系统的文件资源管理器显示完整路径呢?

Windows8文件资源管理器窗口中文件路径的显示方式为只显示当前窗口文件所在文件夹的名称,如果你觉得不喜欢,想要显示该文件的完整路径? 没问题,今天就来教大家怎样在文件资源管理器窗口顶栏上显示该窗口中文件的完整路径. 具体操作方法如下: 1.Win+E打开资源管理器(或者直接双击桌面图标"计算机"),打开窗口. 随便打开一个路径,我们可以看到效果 2.点击菜单栏 - 查看 - 选项 3.打开选项 - 查看,找到"在标题栏中显示完整路径",并在前面打勾 好了,任务完

在Win8系统下清除文件资源管理器中常用位置的方法

在Win8系统下清除文件资源管理器中常用位置的方法: 在Win8的文件资源管理器中,我们单击"文件",可以在右侧看到一个"常用位置"的列表.我们可以在其中找到我们经常访问的位置,方便我们的访问.如果你不希望其它用户在访问你的电脑时看到这些位置信息,我们可以将该列表清空. 具体操作方法如下: 首先打开文件资源管理器, 需要注意的是,"删除历史记录"中的两个选项无法对"常用位置"信息造成任何改变. 我们需要的是,在任务栏,单击右键

Win8.1在文件资源管理器中搜索Word文档的方法

  Win8.1在文件资源管理器中搜索Word文档的方法           具体方法如下: 1.打开文件资源管理器,例如打开E盘,右上角会有搜索框; 2.现在想搜索文件,在搜索框中输入搜索的内容,可以输入文件名直接搜索.如果不知道文件名,可以在搜索框中输入"*.扩展名"来搜索,例想搜索Word文档,可以输入"*.doc"来搜索; 3.搜索功能会在你查看位置的所有文件夹和子文件夹中查找扩展名含doc的文件; 4.单击"搜索"选项卡.如何知道大约修

Win8.1在文件资源管理器中搜索文件

  若要在文件资源管理器中搜索文件,请打开文件资源管理器并使用右上角的搜索框. 通过以下方式打开"这台电脑":从屏幕的右边缘向中间轻扫,点击"搜索"(如果使用鼠标,则指向屏幕的右上角,然后将鼠标指针向下移动,再单击"搜索"),在搜索框中输入这台电脑,然后点击或单击"这台电脑". 搜索功能会在你查看位置的所有文件夹和子文件夹中查找. 在搜索框内点击或单击时,将显示"搜索工具"选项卡. 输入搜索词后,将开始显示