⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 一个分割合并文件的程序
💻 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   ;
using System.Windows.Forms.Design ;


namespace 文件分割
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1 ;
		private System.Windows.Forms.Label label1 ;
		private System.Windows.Forms.ComboBox comboBox1 ;
		private System.Windows.Forms.Label label2 ;
		private System.Windows.Forms.TextBox textBox1 ;
		private System.Windows.Forms.Button button2 ;
		private System.Windows.Forms.OpenFileDialog openFileDialog1 ;
		private System.Windows.Forms.TextBox textBox2 ;
		private System.Windows.Forms.Label label3 ;
		private System.Windows.Forms.ProgressBar progressBar1 ;
		private System.Windows.Forms.Label label4 ;
		/// <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.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button2 = new System.Windows.Forms.Button();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.label4 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Enabled = false;
            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button1.Location = new System.Drawing.Point(145, 137);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(82, 39);
            this.button1.TabIndex = 0;
            this.button1.Text = "执行";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(31, 98);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(109, 25);
            this.label1.TabIndex = 1;
            this.label1.Text = "请选择大小:";
            // 
            // comboBox1
            // 
            this.comboBox1.Items.AddRange(new object[] {
            "720",
            "1440",
            "2880"});
            this.comboBox1.Location = new System.Drawing.Point(122, 95);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(136, 20);
            this.comboBox1.TabIndex = 2;
            this.comboBox1.Text = "1440";
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(31, 20);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(111, 25);
            this.label2.TabIndex = 3;
            this.label2.Text = "请选择文件:";
            // 
            // textBox1
            // 
            this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.textBox1.Location = new System.Drawing.Point(122, 16);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(188, 21);
            this.textBox1.TabIndex = 4;
            // 
            // button2
            // 
            this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.button2.Location = new System.Drawing.Point(316, 15);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(34, 25);
            this.button2.TabIndex = 5;
            this.button2.Text = "...";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // textBox2
            // 
            this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.textBox2.Location = new System.Drawing.Point(122, 54);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(188, 21);
            this.textBox2.TabIndex = 7;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(19, 59);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(109, 25);
            this.label3.TabIndex = 6;
            this.label3.Text = "输入存放目录:";
            // 
            // progressBar1
            // 
            this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.progressBar1.Location = new System.Drawing.Point(0, 183);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(403, 25);
            this.progressBar1.TabIndex = 9;
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(266, 98);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(92, 25);
            this.label4.TabIndex = 10;
            this.label4.Text = "(单位:K)";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(403, 208);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.progressBar1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.comboBox1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.Text = "文件分割";
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main ( )  
		{
			Application.Run ( new Form1 ( )  )  ;
		}
		/// <summary>
		/// 
		/// </summary>


		private void button1_Click ( object sender , System.EventArgs e ) 
		{
			int iFileSize= Int32.Parse  ( comboBox1.Text )  *1024 ;  
			//根据选择来设定分割的小文件的大小
			if ( Directory.Exists ( textBox2.Text ) )  
				Directory.Delete ( textBox2.Text ,true )  ; 
				//如果计算机存在存放分割文件的目录,则全部删除此目录所有文件
			Directory.CreateDirectory ( textBox2.Text )  ; 
				//反之则在计算机创建目录
			FileStream SplitFileStream=new FileStream ( textBox1.Text , FileMode.Open )  ;  
			//以文件的全路对应的字符串和文件打开模式来初始化FileStream文件流实例
			BinaryReader SplitFileReader=new BinaryReader ( SplitFileStream )  ; 
			//以FileStream文件流来初始化BinaryReader文件阅读器
			byte [ ] TempBytes ;  
			//每次分割读取的最大数据
			int iFileCount = ( int ) ( SplitFileStream.Length / iFileSize ) ;  
			//小文件总数
			progressBar1.Maximum = iFileCount ;
			if ( SplitFileStream.Length % iFileSize != 0 )  iFileCount++ ;
			string [ ] TempExtra = textBox1.Text.Split ( '.' )  ;
			/*  循环将大文件分割成多个小文件  */
			for ( int i = 1 ; i <= iFileCount ; i++ ) 
			{
				string sTempFileName = textBox2.Text + @"\"+i.ToString ( ) .PadLeft ( 4 ,'0' ) + "." + TempExtra[ TempExtra.Length - 1 ] ;  //小文件名
				//确定小文件的文件名称
				FileStream TempStream = new FileStream ( sTempFileName , FileMode.OpenOrCreate )  ;
				//根据文件名称和文件打开模式来初始化FileStream文件流实例
				BinaryWriter TempWriter = new BinaryWriter ( TempStream )  ;  
				//以FileStream实例来创建、初始化BinaryWriter书写器实例
				TempBytes = SplitFileReader.ReadBytes ( iFileSize )  ;       
				//从大文件中读取指定大小数据
				TempWriter.Write ( TempBytes )  ; 
				//把此数据写入小文件
				TempWriter.Close ( )  ;    
				//关闭书写器,形成小文件
				TempStream.Close ( )  ;  
				//关闭文件流
				progressBar1.Value = i - 1 ;

			}
			SplitFileReader.Close ( )  ;    
			//关闭大文件阅读器
			SplitFileStream.Close ( )  ;   
			MessageBox.Show  ( "分割成功!" )  ;
			progressBar1.Value = 0 ;
		}

		private void button2_Click ( object sender , System.EventArgs e ) 
		{
			openFileDialog1.Title = "请选择要分割的文件名称" ;
			System.Windows.Forms.DialogResult drTemp = openFileDialog1.ShowDialog  ( )  ;
			if  ( drTemp == DialogResult.OK && openFileDialog1.FileName != string.Empty  ) 
			{
				textBox1.Text = openFileDialog1.FileName  ;
				button1.Enabled = true ;
			}
			
		}

		private void button3_Click ( object sender , System.EventArgs e ) 
		{
			//openFileDialog1.Title ="请选择文件夹" ;
		}

		private void comboBox1_SelectedIndexChanged ( object sender , System.EventArgs e ) 
		{
		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -