📄 form1.cs
字号:
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.IO ;
using System.Text ;
using System.Data.OleDb ;
namespace 合并程序
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ProgressBar progressBar1 ;
private System.Windows.Forms.TextBox textBox2 ;
private System.Windows.Forms.Label label3 ;
private System.Windows.Forms.Button button2 ;
private System.Windows.Forms.TextBox textBox1 ;
private System.Windows.Forms.Label label2 ;
private System.Windows.Forms.Button button1 ;
private System.Windows.Forms.OpenFileDialog openFileDialog1 ;
public string sDirectoryName = "" ;
//定义一个全局变量,用以存放合并文件所在的目录
/// <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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent ( )
{
this.progressBar1 = new System.Windows.Forms.ProgressBar ( ) ;
this.textBox2 = new System.Windows.Forms.TextBox ( ) ;
this.label3 = new System.Windows.Forms.Label ( ) ;
this.button2 = new System.Windows.Forms.Button ( ) ;
this.textBox1 = new System.Windows.Forms.TextBox ( ) ;
this.label2 = new System.Windows.Forms.Label ( ) ;
this.button1 = new System.Windows.Forms.Button ( ) ;
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog ( ) ;
this.SuspendLayout ( ) ;
this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom ;
this.progressBar1.Location = new System.Drawing.Point ( 0 , 160 ) ;
this.progressBar1.Name = "progressBar1" ;
this.progressBar1.Size = new System.Drawing.Size ( 353 , 23 ) ;
this.progressBar1.TabIndex = 16 ;
this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle ;
this.textBox2.Location = new System.Drawing.Point ( 120 , 47 ) ;
this.textBox2.Name = "textBox2" ;
this.textBox2.Size = new System.Drawing.Size ( 171 , 21 ) ;
this.textBox2.TabIndex = 15 ;
this.textBox2.Text = "" ;
this.label3.Location = new System.Drawing.Point ( 25 , 52 ) ;
this.label3.Name = "label3" ;
this.label3.TabIndex = 14 ;
this.label3.Text = "输入文件名:" ;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup ;
this.button2.Location = new System.Drawing.Point ( 296 , 11 ) ;
this.button2.Name = "button2" ;
this.button2.Size = new System.Drawing.Size ( 32 , 23 ) ;
this.button2.TabIndex = 13 ;
this.button2.Text = "..." ;
this.button2.Click += new System.EventHandler ( this.button2_Click ) ;
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle ;
this.textBox1.Location = new System.Drawing.Point ( 120 , 12 ) ;
this.textBox1.Name = "textBox1" ;
this.textBox1.Size = new System.Drawing.Size ( 171 , 21 ) ;
this.textBox1.TabIndex = 12 ;
this.textBox1.Text = "" ;
this.label2.Location = new System.Drawing.Point ( 37 , 16 ) ;
this.label2.Name = "label2" ;
this.label2.TabIndex = 11 ;
this.label2.Text = "选择文件:" ;
this.button1.Enabled = false ;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat ;
this.button1.Location = new System.Drawing.Point ( 134 , 95 ) ;
this.button1.Name = "button1" ;
this.button1.Size = new System.Drawing.Size ( 75 , 36 ) ;
this.button1.TabIndex = 10 ;
this.button1.Text = "执行" ;
this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
this.ClientSize = new System.Drawing.Size ( 353 , 183 ) ;
this.Controls.AddRange ( new System.Windows.Forms.Control[] {
this.progressBar1 ,
this.textBox2 ,
this.label3 ,
this.button2 ,
this.textBox1 ,
this.label2 ,
this.button1} ) ;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle ;
this.MaximizeBox = false ;
this.Name = "Form1" ;
this.Text = "合并程序" ;
this.ResumeLayout ( false ) ;
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void button2_Click ( object sender , System.EventArgs e )
{
openFileDialog1.Title ="请选择要合并的第一个文件" ;
System.Windows.Forms.DialogResult drTemp = openFileDialog1.ShowDialog ( ) ;
if ( drTemp == DialogResult.OK && openFileDialog1.FileName != "" )
{
textBox1.Text = openFileDialog1.FileName ;
}
string[ ] path = openFileDialog1.FileName .Split ( @"\".ToCharArray ( ) ) ;
string sTemp = "" ;
int i= 0 ;
for ( i= 0 ; i < path.Length - 1 ; i++ )
{
sTemp = sTemp + path[ i ] + @"\" ;
}
button1.Enabled = true ;
sDirectoryName= sTemp ;
//获得文件所在目录
}
private void button1_Click ( object sender , System.EventArgs e )
{
string[ ] arrFileNames = Directory.GetFiles ( sDirectoryName ) ;
//获取存放分割后小文件所在目录所在的所有小文件
int iSumFile = arrFileNames.Length ;
progressBar1.Maximum = iSumFile ;
FileStream AddStream = new FileStream ( textBox2.Text , FileMode.OpenOrCreate ) ;
//以合并后的文件名称和打开方式来创建、初始化FileStream文件流
BinaryWriter AddWriter = new BinaryWriter ( AddStream ) ;
//以FileStream文件流来初始化BinaryWriter书写器,此用以合并分割的文件
/*循环合并小文件,并生成合并文件 */
for ( int i = 0 ; i < iSumFile ; i++ )
{
FileStream TempStream = new FileStream ( arrFileNames[ i ] , FileMode.Open ) ;
//以小文件所对应的文件名称和打开模式来初始化FileStream文件流,起读取分割作用
BinaryReader TempReader = new BinaryReader ( TempStream ) ;
//用FileStream文件流来初始化BinaryReader文件阅读器,也起读取分割文件作用
AddWriter.Write ( TempReader.ReadBytes ( ( int ) TempStream.Length ) ) ;
//读取分割文件中的数据,并生成合并后文件
TempReader.Close ( ) ;
//关闭BinaryReader文件阅读器
TempStream.Close ( ) ;
//关闭FileStream文件流
progressBar1.Value = i + 1 ;
//显示合并进程
}
AddWriter.Close ( ) ;
//关闭BinaryWriter文件书写器
AddStream.Close ( ) ;
//关闭FileStream文件流
MessageBox.Show ( "成功合并!" ) ;
progressBar1.Value = 0 ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -