文件分割器

本文介绍了一个用于将大型文件分割成多个小文件的工具的使用方法。包括如何选择源文件、设定分割大小以及指定目标文件夹。通过简单的步骤,用户可以轻松地管理大文件的存储和传输。

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace prjSplit
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            DialogResult dr = ofd.ShowDialog();
            if (dr==DialogResult.Cancel)
            {
                return;
            }
            string fileName = ofd.FileName;
            this.txtSourceFile.Text = fileName;
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult dr = fbd.ShowDialog();
            if (dr==DialogResult.Cancel)
            {
                return;
            }
            string path = fbd.SelectedPath;
            this.txtFolder.Text = path;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            string fileName = this.txtSourceFile.Text;
            string s = this.txtSize.Text;
            string path = this.txtFolder.Text;
            if (!CheckFileName(fileName))
            {
                return;
            }
            if (!CheckSize(s))
            {
                return;
            }
            if (!CheckPath(path))
            {
                return;
            }
            SplitFile(fileName,int.Parse(s),path);
        }

        private void SplitFile(string fileName, int size, string path)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open);
            size = size * 1024;
            byte[] bs = new byte[size];
            int i=0;
            while (true)
            {
                i++;
                int x=fs.Read(bs, 0, bs.Length);
                if (x==0)
                {
                    break;
                }

                FileStream fsOut = new FileStream(path+"\\"+i.ToString()+".part",FileMode.Create);
                fsOut.Write(bs, 0, x);
                fsOut.Flush();
                fsOut.Close();
            }

            fs.Close();
        }

        private bool CheckPath(string path)
        {
            if (path == "")
            {
                MessageBox.Show("没有选择目标文件夹。");
                return false;
            }
            if (!Directory.Exists(path))
            {
                MessageBox.Show("目标文件夹不存在。");
                return false;
            }
            return true;
        }

        private bool CheckSize(string s)
        {
            if (s.Trim()=="")
            {
                MessageBox.Show("大小必须填写。");
                return false;
            }
            try
            {
                int.Parse(s);
            }
            catch (Exception)
            {
                MessageBox.Show("大小必须是数字。");
                return false;
            }
            int size = int.Parse(s);
            if (size<=0)
            {
                MessageBox.Show("大小必须是正数。");
                return false;
            }
            return true;
        }

        private bool CheckFileName(string fileName)
        {
            if (fileName=="")
            {
                MessageBox.Show("没有选择源文件。");
                return false;
            }
            if (!File.Exists(fileName))
            {
                MessageBox.Show("源文件不存在。");
                return false;
            }
            return true;
            
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值