首页  >  文章  >  后端开发  >  C#实现一次分割多个文件

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

大家讲道理
大家讲道理原创
2016-11-10 10:17:211715浏览

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();
    }
}

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn