1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace FileStrem大文件分割复制
  14. {
  15. public partial class Form1 : Form
  16. {
  17. private int WriterByetNub = ;//100M复制速度
  18. //源目标
  19. private FileStream FileToRead;
  20. //复制到文件
  21. private FileStream FileToWrite;
  22. //保存文件的地址
  23. private string SaveFile_Add;
  24. //源文件的名字
  25. private string File_Add;
  26. //设置正常写入字节
  27. private Byte[] byteToWrite;
  28. //设置剩余写入字节
  29. private Byte[] byteToLastWrite;
  30. //循环次数
  31. private long WriteTimes;
  32. //循环后的剩余字节
  33. private int L_Size;
  34.  
  35. public Form1()
  36. {
  37. InitializeComponent();
  38. }
  39. //设置委托
  40. private delegate void OpenFile();
  41.  
  42. private void Cpy()
  43. {
  44. try
  45. {
  46. label_Add.Text = "源地址";
  47.  
  48. label_Cpy_Add.Text = "复制到";
  49.  
  50. label_Cpy_Lc.Text = "复制进程:";
  51.  
  52. label_Write.Text = "已经写入";
  53.  
  54. label_FileSize.Text = "源文件总大小";
  55. //文件选取
  56. OpenFileDialog openfileDialog = new OpenFileDialog();
  57. //show文件选取器
  58. openfileDialog.ShowDialog();
  59.  
  60. File_Add = openfileDialog.FileName;
  61.  
  62. label_Add.Text += ":" + File_Add;
  63.  
  64. //保存地址选取
  65. FolderBrowserDialog savefileDialog = new FolderBrowserDialog();
  66.  
  67. savefileDialog.ShowDialog();
  68.  
  69. SaveFile_Add = savefileDialog.SelectedPath;
  70.  
  71. label_Cpy_Add.Text += ":" + SaveFile_Add + File_Add;
  72.  
  73. FileToRead = new FileStream(File_Add, FileMode.Open, FileAccess.Read);
  74.  
  75. FileToWrite = new FileStream(@SaveFile_Add + "\\" + openfileDialog.SafeFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  76.  
  77. label_FileSize.Text = "源文件总大小"+(FileToRead.Length/).ToString()+"KB";
  78. if (FileToRead.Length > WriterByetNub)
  79. //设置写入字节数组
  80. {
  81. byteToWrite = new byte[WriterByetNub];
  82. //循环次数
  83. WriteTimes = FileToRead.Length / WriterByetNub;
  84. //多次循环后剩余字节
  85. L_Size = Convert.ToInt32(FileToRead.Length % WriterByetNub);
  86. //多次循环后字节数组
  87. byteToLastWrite = new byte[L_Size];
  88.  
  89. for (long i = ; i <= WriteTimes; i++)
  90. {
  91. //读源文件
  92. FileToRead.Read(byteToWrite, , WriterByetNub);
  93.  
  94. //写数据到目标文件
  95. FileToWrite.Write(byteToWrite, , WriterByetNub);
  96.  
  97. //设置进度条的值
  98. progressBar.Value = Convert.ToInt32(i * / WriteTimes);
  99.  
  100. Application.DoEvents();
  101.  
  102. //设置Lable上的进度值
  103. label_Cpy_Lc.Text = "复制进程:" + Convert.ToInt32((i * ) / WriteTimes).ToString() + "%";
  104.  
  105. //设置写入值
  106. label_Write.Text = "已写入" + (FileToRead.Position / ).ToString() + "KB";
  107. }
  108.  
  109. //剩余字节的读和写
  110. if (L_Size != )
  111. {
  112. FileToRead.Read(byteToLastWrite, , L_Size);
  113.  
  114. FileToWrite.Write(byteToLastWrite, , L_Size);
  115. }
  116. }
  117. else
  118. {
  119. progressBar.Maximum =(int) FileToRead.Length;
  120. byteToWrite = new byte[FileToRead.Length];
  121. FileToRead.Read(byteToWrite, , (int)FileToRead.Length);
  122. label_Cpy_Lc.Text = "复制进程:" + Convert.ToInt32(FileToRead.Position/FileToRead.Length*).ToString() + "%";
  123.  
  124. //设置写入值
  125. label_Write.Text = "已写入" + (FileToRead.Position / ).ToString() + "KB";
  126. progressBar.Value =(int )FileToRead.Position;
  127. FileToWrite.Write(byteToWrite, , (int)FileToRead.Length);
  128. }
  129. FileToRead.Flush();
  130.  
  131. FileToWrite.Flush();
  132.  
  133. FileToRead.Close();
  134.  
  135. FileToWrite.Close();
  136.  
  137. MessageBox.Show("复制完成");
  138. }
  139. catch(Exception ex)
  140.  
  141. {
  142. FileToRead.Flush();
  143.  
  144. FileToWrite.Flush();
  145.  
  146. FileToRead.Close();
  147.  
  148. FileToWrite.Close();
  149.  
  150. MessageBox.Show(ex.ToString());
  151.  
  152. }
  153. }
  154.  
  155. private void openFileBtn_Click(object sender, EventArgs e)
  156. {
  157. OpenFile getFile = new OpenFile(Cpy);
  158. this.Invoke(getFile);
  159. }
  160.  
  161. private void button2_Click(object sender, EventArgs e)
  162. {
  163. OpenFileDialog openFileDialog1 = new OpenFileDialog();
  164. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  165. {
  166. textBox1.Text = openFileDialog1.FileName;
  167. }
  168. }
  169. private void button3_Click(object sender, EventArgs e)
  170. {
  171. FolderBrowserDialog f = new FolderBrowserDialog();
  172. if (f.ShowDialog() == DialogResult.OK)
  173. {
  174. textBox2.Text = f.SelectedPath +@"\"+Path.GetFileName(textBox1.Text.Trim());
  175. }
  176. }
  177.  
  178. private void button1_Click(object sender, EventArgs e)
  179. {
  180. ApiCopyFile.DoCopy(textBox1.Text.Trim(), textBox2.Text.Trim());
  181. }
  182.  
  183. }
  184. public class ApiCopyFile
  185. {
  186. private const int FO_COPY = 0x0002;
  187. private const int FOF_ALLOWUNDO = 0x00044;
  188. //显示进度条 0x00044 // 不显示一个进度对话框 0x0100 显示进度对话框单不显示进度条 0x0002显示进度条和对话框
  189. private const int FOF_SILENT = 0x0002;//0x0100;
  190. //
  191. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = )]
  192. public struct SHFILEOPSTRUCT
  193. {
  194. public IntPtr hwnd;
  195. [MarshalAs(UnmanagedType.U4)]
  196. public int wFunc;
  197. public string pFrom;
  198. public string pTo;
  199. public short fFlags;
  200. [MarshalAs(UnmanagedType.Bool)]
  201. public bool fAnyOperationsAborted;
  202. public IntPtr hNameMappings;
  203. public string lpszProgressTitle;
  204. }
  205. [DllImport("shell32.dll", CharSet = CharSet.Auto)]
  206. static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
  207. public static bool DoCopy(string strSource, string strTarget)
  208. {
  209. SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();
  210. fileop.wFunc = FO_COPY;
  211. fileop.pFrom = strSource;
  212. fileop.lpszProgressTitle = "复制大文件";
  213. fileop.pTo = strTarget;
  214. //fileop.fFlags = FOF_ALLOWUNDO;
  215. fileop.fFlags = FOF_SILENT;
  216.  
  217. return SHFileOperation(ref fileop) == ;
  218. }
  219. }
  220. }

FileStrem大文件分割复制的更多相关文章

  1. c# 大文件分割 复制 Filestream 进度条

    大文件分割复制,每次复制100M 也可以复制别的较大数值. 小于1G的小文件就直接复制得了.代码里没写 ,但是很简单 直接写进去就好了,难得是分割复制 所以没写. 好吧 我还是改了 改成小文件也可以复 ...

  2. c#大文件分割过程

    需求: 在项目开发中,我们会遇到单个文件大小超过1TB的文件,这样的文件只能进行单文件读取,往往会造成读取完成耗时过长,导致客户在使用体验过程中不满意. 为了解决提升大文件的解析速度,我想到了先分割大 ...

  3. android下大文件分割上传

    由于android自身的原因,对大文件(如影视频文件)的操作很容易造成OOM,即:Dalvik堆内存溢出,利用文件分割将大文件分割为小文件可以解决问题. 文件分割后分多次请求服务. //文件分割上传 ...

  4. PHP + JS 实现大文件分割上传

    服务器上传文件会有一定的限制.避免内存消耗过大影响性能,在 php.ini 配置文件中,有几个影响参数: upload_max_filesize = 2M //PHP最大能接受的文件大小 post_m ...

  5. Linux大文件分割splite

    /********************************************************************** * Linux大文件分割splite * 说明: * 编 ...

  6. Html5 突破微信限制实现大文件分割上传

    先来前端代码 <!DOCTYPE html> <html> <head> <meta name="viewport" content=&q ...

  7. 大文件分割、命令脚本 - Python

    日志文件分割.命名 工作中经常会收到测试同学.客户同学提供的日志文件,其中不乏几百M一G的也都有,毕竟压测一晚上产生的日志量还是很可观的,xDxD,因此不可避免的需要对日志进行分割,通常定位问题需要针 ...

  8. Linux中split大文件分割和cat合并文件

    当需要将较大的数据上传到服务器,或从服务器下载较大的日志文件时,往往会因为网络或其它原因而导致传输中断而不得不重新传输.这种情况下,可以先将大文件分割成小文件后分批传输,传完后再合并文件. 1.分割 ...

  9. formdata方式上传文件,支持大文件分割上传

    1.upload.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/html"> <h ...

随机推荐

  1. Kafka 和 ZooKeeper 的分布式消息队列分析

    1. Kafka 总体架构 基于 Kafka-ZooKeeper 的分布式消息队列系统总体架构如下: 如上图所示,一个典型的 Kafka 体系架构包括若干 Producer(消息生产者),若干 bro ...

  2. Delphi7-TClientDataSet: 查找

    TClientDataSet[12]: 查找 方法有:1.Locate: 根据字段列表和对应的字段值查找并定位, 找到返回 True.2.Lookup: 根据字段列表和对应的字段值查找, 返回需要的字 ...

  3. Pearson Correlation Score

    [http://www.statisticshowto.com/what-is-the-pearson-correlation-coefficient/] Correlation between se ...

  4. NSubstitute.Analyzers检测NSubstitute用法冲突

    NSubstitute是一个.Net环境使用的,简洁,语法友好的Mock库.语法简洁的缺点是有一些失败的用法很难察觉和检测.比如试图mock一个非虚拟成员-NSubstitute不能看到这些成员所以不 ...

  5. vue路由传参的几种基本方式

    原文地址 this.$router.push跳转 现有如下场景,点击父组件的li元素跳转到子组件中,并携带参数,便于子组件获取数据.父组件中: <li v-for="article i ...

  6. web赛题2

    @上海赛wp 微信 和 https://www.ctfwp.com/articals/2019unctf.html 后续公告https://unctf.buuoj.cn/notice.html 必看! ...

  7. 【C/C++开发】【Java开发】JNI的替代者—使用JNA访问Java外部功能接口

    JNI的替代者-使用JNA访问Java外部功能接口 1. JNA简单介绍 先说JNI(Java Native Interface)吧,有过不同语言间通信经历的一般都知道,它允许Java代码和其他语言( ...

  8. Tensorflow之实现物体检测

    目录 项目背景 TensorFlow介绍 环境搭建 模型选用 Api使用说明 运行路由 小结 项目背景 产品看到竞品可以标记物体的功能,秉承一贯的他有我也要有,他没有我更要有的作风,丢过来一网站,说这 ...

  9. python 指定文件夹下所有文件(包括子目录下的文件)拷贝到目标文件夹下

    #!/usr/bin/env python3 # -*- coding:utf8 -*- # @TIME :2018/9/17 9:02 # @Author:dazhan # @File :copyf ...

  10. Appendix 2- Lebesgue integration and Reimann integration

    Lebesgue integration and Reimann integration: Reimann: Split up the axis into equal intervals, then ...