Heim  >  Artikel  >  Backend-Entwicklung  >  C#实现一次分割多个文件

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

大家讲道理
大家讲道理Original
2016-11-10 10:17:211666Durchsuche

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:C# 将 HTML 转成纯文本Nächster Artikel:C#文件合并代码