c# 大文件分割 复制 Filestream 进度条
大文件分割复制,每次复制100M 也可以复制别的较大数值。
小于1G的小文件就直接复制得了。代码里没写 ,但是很简单 直接写进去就好了,难得是分割复制 所以没写. 好吧 我还是改了 改成小文件也可以复制了,全面一点是不
效果图【图片里的总文件大小 忘记除1024了。 文章里的代码改了,图片和源代码文件改了 】
代码写得不是很好.请多多指教
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace FileStrem大文件分割复制 { public partial class Form1 : Form { private int WriterByetNub = 104857600;//100M复制速度 //源目标 private FileStream FileToRead; //复制到文件 private FileStream FileToWrite; //保存文件的地址 private string SaveFile_Add; //源文件的名字 private string File_Add; //设置正常写入字节 private Byte[] byteToWrite; //设置剩余写入字节 private Byte[] byteToLastWrite; //循环次数 private long WriteTimes; //循环后的剩余字节 private int L_Size; public Form1() { InitializeComponent(); } //设置委托 private delegate void OpenFile(); private void Cpy() { try { label_Add.Text = "源地址"; label_Cpy_Add.Text = "复制到"; label_Cpy_Lc.Text = "复制进程:"; label_Write.Text = "已经写入"; label_FileSize.Text = "源文件总大小"; //文件选取 OpenFileDialog openfileDialog = new OpenFileDialog(); //show文件选取器 openfileDialog.ShowDialog(); File_Add = openfileDialog.FileName; label_Add.Text += ":" + File_Add; //保存地址选取 FolderBrowserDialog savefileDialog = new FolderBrowserDialog(); savefileDialog.ShowDialog(); SaveFile_Add = savefileDialog.SelectedPath; label_Cpy_Add.Text += ":" + SaveFile_Add + File_Add; FileToRead = new FileStream(File_Add, FileMode.Open, FileAccess.Read); FileToWrite = new FileStream(@SaveFile_Add + "\\" + openfileDialog.SafeFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); label_FileSize.Text = "源文件总大小"+(FileToRead.Length/1024).ToString()+"KB"; if (FileToRead.Length > WriterByetNub) //设置写入字节数组 { byteToWrite = new byte[WriterByetNub]; //循环次数 WriteTimes = FileToRead.Length / WriterByetNub; //多次循环后剩余字节 L_Size = Convert.ToInt32(FileToRead.Length % WriterByetNub); //多次循环后字节数组 byteToLastWrite = new byte[L_Size]; for (long i = 0; i <= WriteTimes; i++) { //读源文件 FileToRead.Read(byteToWrite, 0, WriterByetNub); //写数据到目标文件 FileToWrite.Write(byteToWrite, 0, WriterByetNub); //设置进度条的值 progressBar.Value = Convert.ToInt32(i * 100 / WriteTimes); Application.DoEvents(); //设置Lable上的进度值 label_Cpy_Lc.Text = "复制进程:" + Convert.ToInt32((i * 100) / WriteTimes).ToString() + "%"; //设置写入值 label_Write.Text = "已写入" + (FileToRead.Position / 1024).ToString() + "KB"; } //剩余字节的读和写 if (L_Size != 0) { FileToRead.Read(byteToLastWrite, 0, L_Size); FileToWrite.Write(byteToLastWrite, 0, L_Size); } } else //当写于分割的字节大小时 直接复制吧
{ //设置进度条的最大值 progressBar.Maximum =(int) FileToRead.Length; //设置字节 byteToWrite = new byte[FileToRead.Length]; //读取到字节中 FileToRead.Read(byteToWrite, 0, (int)FileToRead.Length); //计算进程 label_Cpy_Lc.Text = "复制进程:" + Convert.ToInt32(FileToRead.Position/FileToRead.Length*100).ToString() + "%"; //设置写入值 label_Write.Text = "已写入" + (FileToRead.Position / 1024).ToString() + "KB"; //计算进度条的进度 progressBar.Value =(int )FileToRead.Position; //写入完成 FileToWrite.Write(byteToWrite, 0, (int)FileToRead.Length); } FileToRead.Flush(); FileToWrite.Flush(); FileToRead.Close(); FileToWrite.Close(); MessageBox.Show("复制完成"); } catch(Exception ex) { FileToRead.Flush(); FileToWrite.Flush(); FileToRead.Close(); FileToWrite.Close(); MessageBox.Show(ex.ToString()); } } private void openFileBtn_Click(object sender, EventArgs e) { OpenFile getFile = new OpenFile(Cpy); this.Invoke(getFile); } } }