C#实现一次分割多个文件

大家讲道理
发布: 2016-11-10 10:17:21
原创
1666 人浏览过

using System.IO;
using System.Windows.Forms;
 
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter ="All files (*.*)|*.*";
dlg.FilterIndex = 1;
dlg.RestoreDirectory = true;
dlg.Multiselect = true;
if (dlg.ShowDialog() == DialogResult.OK)
{
    foreach (string filetosplit in dlg.FileNames)
    {
            string targetpath="D:";
            FileStream fsr = new FileStream(filetosplit, FileMode.Open, FileAccess.Read);
            long FileLength=fsr.Length;
            byte[] btArr = new byte[FileLength];
            fsr.Read(btArr, 0, (int)FileLength);
            fsr.Close();
            long PartLength=FileLength/3+FileLength%3;
            int nCount=(int)Math.Ceiling((double)FileLength/PartLength);
            string strFileName=Path.GetFileName(filetosplit);
            long byteCount=0;
            for(int i=1;i<=nCount;i++,byteCount=(i<nCount?byteCount+PartLength:FileLength-PartLength))
            {
                FileStream fsw = new FileStream(targetpath + Path.DirectorySeparatorChar+ strFileName +i, FileMode.Create, FileAccess.Write);
                fsw.Write(btArr, (int)byteCount, (int)(i<nCount?PartLength:FileLength-byteCount));
                fsw.Flush();
                fsw.Close();
            }
            fsr.Close();
    }
}
登录后复制

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!