C# 文件拆分器

此博客展示了一个用C#编写的文件拆分程序代码。该程序基于Windows窗体,用户可通过界面选择文件和拆分尺寸,程序会将文件按指定大小拆分,并生成组合文件的批处理文件。若操作失误,会给出相应提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



}
}

资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 华为移动服务(Huawei Mobile Services,简称 HMS)是一个全面开放的移动服务生态系统,为企业和开发者提供了丰富的工具和 API,助力他们构建、运营和推广应用。其中,HMS Scankit 是华为推出的一款扫描服务 SDK,支持快速集成到安卓应用中,能够提供高效且稳定的二维码和条形码扫描功能,适用于商品扫码、支付验证、信息获取等多种场景。 集成 HMS Scankit SDK 主要包括以下步骤:首先,在项目的 build.gradle 文件中添加 HMS Core 库和 Scankit 依赖;其次,在 AndroidManifest.xml 文件中添加相机访问和互联网访问权限;然后,在应用程序的 onCreate 方法中调用 HmsClient 进行初始化;接着,可以选择自定义扫描界面或使用 Scankit 提供的默认扫描界面;最后,实现 ScanCallback 接口以处理扫描成功和失败的回调。 HMS Scankit 内部集成了开源的 Zxing(Zebra Crossing)库,这是一个功能强大的条码和二维码处理库,提供了解码、生成、解析等多种功能,既可以单独使用,也可以与其他扫描框架结合使用。在 HMS Scankit 中,Zxing 经过优化,以更好地适应华为设备,从而提升扫描性能。 通常,ScanKitDemoGuide 包含了集成 HMS Scankit 的示例代码,涵盖扫描界面的布局、扫描操作的启动和停止以及扫描结果的处理等内容。开发者可以参考这些代码,快速掌握在自己的应用中实现扫码功能的方法。例如,启动扫描的方法如下: 处理扫描结果的回调如下: HMS Scankit 支持所有安卓手机,但在华为设备上能够提供最佳性能和体验,因为它针对华为硬件进行了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值