Home  >  Article  >  Backend Development  >  C# implements splitting multiple files at one time

C# implements splitting multiple files at one time

大家讲道理
大家讲道理Original
2016-11-10 10:17:211667browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn